What does "TypeError:can't modify frozen string" mean?
I got this error when using the Twitter gem, and passing in an array.
My code looks innocent enough - wondering what is causing this?
def twitter_get_users(client, user_names=[])
copy=Array.new(user_names)
users = client.users(user_names)
开发者_开发问答end
TypeError: can't modify frozen string
from gems/twitter-1.0.0/lib/twitter/client/utils.rb:10:in `gsub!'
from /gems/twitter-1.0.0/lib/twitter/client/utils.rb:10:in `clean_screen_name!'
from /gems/twitter-1.0.0/lib/twitter/client/utils.rb:33:in `merge_users_into_options!'
Try this and see if it helps:
def twitter_get_users(client, user_names=[])
client.users user_names.map(&:dup)
end
Ruby allows you to freeze objects so that they may not be mutated. Either the Twitter gem froze a string and then tried to call gsub!
on it, or you passed in an already-frozen string.
This answer doesn't help you solve the root of your problem, but it does answer the questions of "What does this mean and why is it happening?"
- Did you try opening up
gems/twitter-1.0.0/lib/twitter/client/utils.rb
and looking at line 10? :-) - if you can't find anything obvious in there, try
grep -r '.freeze'
on your rails project and on that gem's directory (gems/twitter-1.0.0/
)
精彩评论