From: Scott Baldwin Date: 2006-08-20T15:54:32+09:00 Subject: Re: Defining a method []= On 8/20/06, Brett Kushner wrote: > I have used a method of [] with "def method[](string)". But I can't > figure out how to use[]=. > > An array works with []= such as array['string1'] = 'string2' so how > would I write a method like that for my own class? > > > -- > Posted via http://www.ruby-forum.com/. > > You define a method called "[]=". class Foo def []= key, value puts "Setting key '#{key}' to '#{value}'" end end f = Foo.new f[:hello] = "world" # Setting key 'hello' to 'world'