From: Brian Candler Date: 2010-02-15T18:21:37+09:00 Subject: Re: can't convert Symbol into String Ralph Shnelvar wrote: > I posted the question here because I am asking a Ruby question. > > Let me rephrase the question ... > > Under what circumstances will Ruby not be able to convert a symbol into > a string? In general, Ruby will never convert a Symbol into a String. However: * There are some built-in methods which accept either a Symbol or a String. For example, send(:foo) send("foo") work the same, because send will accept a String and convert it to a Symbol automatically. * Similarly, you may come across some third-party libraries which will accept either a Symbol or a String, and convert automatically if required (perhaps by calling #to_s or #to_sym on the argument). Rails' HashWithIndifferentAccess is an example. * In Ruby 1.9, Symbol does duck-type more closely to a String. # 1.9.2 >> :bar[1] => "a" # 1.8.6 >> :bar[1] NoMethodError: undefined method `[]' for :bar:Symbol from (irb):1 But in general, it's up to you to look at either the documentation or the source code of the thing that you're calling, to see if it requires you to pass a String or a Symbol, or will accept either. HTH, Brian. -- Posted via http://www.ruby-forum.com/.