From: "Brian Schröder" Date: 2005-08-26T21:37:18+09:00 Subject: Re: symbols vs. strings On 26/08/05, Mark Volkmann wrote: > I got the impression that when a method expects a Hash containing > certain keys, I can generally specify the keys using either Symbol or > String objects. So I tried this. > > h = {} > h[:foo] = 19 > h['foo'] = 20 > > and was surprised to see that those are treated as distinct entries in the Hash. > > So how is it that I can sometimes choose whether I want to specify a > given key as either a Symbol or a String? > > For example, when using Tk and w represents some widget, I think I can > say either > > w.pack(:side => 'top') > > or > > w.pack('side' => 'top') > > -- > R. Mark Volkmann > Partner, Object Computing, Inc. > > That can e.g. be implemented like this: def pack(arghash) arghash = arghash.inject({}) { | r, (k, v) | r[k.to_s] = v; r } side = arghash['side'] ... end regards, Brian -- http://ruby.brian-schroeder.de/ Stringed instrument chords: http://chordlist.brian-schroeder.de/