meow
meow

Reputation: 28164

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!'

Upvotes: 0

Views: 1883

Answers (3)

bowsersenior
bowsersenior

Reputation: 12574

Try this and see if it helps:

def twitter_get_users(client, user_names=[])
  client.users user_names.map(&:dup)
end

Upvotes: 3

John Bachir
John Bachir

Reputation: 22731

  1. Did you try opening up gems/twitter-1.0.0/lib/twitter/client/utils.rb and looking at line 10? :-)
  2. 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/)

Upvotes: 1

Phrogz
Phrogz

Reputation: 303261

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?"

Upvotes: 2

Related Questions