I have used a method of [] with “def method”. 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?
I have used a method of [] with “def method”. 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?
Brett K. wrote:
I have used a method of [] with “def method”. 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
On Sun, 20 Aug 2006 02:40:27 -0400, Brett K.
[email protected] wrote:
I have used a method of [] with “def method”. 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?
def method[]=(index, value)
…
end
Regards,
JJ
On 8/20/06, Brett K. [email protected] wrote:
You define a method called “[]=”.
class Foo
def []= key, value
puts “Setting key ‘#{key}’ to ‘#{value}’”
end
end
f = Foo.new
f[:hello] = “world”
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs