Defining a method []=

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/.

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?

Much the same way -- it will have two parameters.

   class MyClass
     def =(index,assigned)
       # ...whatever...
     end
   end

Make sense?

Hal

def method=(index, value)
...
end

Regards,
   JJ

···

On Sun, 20 Aug 2006 02:40:27 -0400, Brett Kushner <brettkushner@hotmail.com> 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?

--
Using Opera's revolutionary e-mail client: Opera Web Browser | Faster, Safer, Smarter | Opera

You define a method called "=".

class Foo
def = key, value
puts "Setting key '#{key}' to '#{value}'"
end

f = Foo.new
f[:hello] = "world"

# Setting key 'hello' to 'world'

···

On 8/20/06, Brett Kushner <brettkushner@hotmail.com> 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/\.