What about {:a => "a", :b => "b"} argument

Hi

I'm doing some gnome/ruby stuff and I wonder how instance method's argument like {:a => "a", :b => "b"} can be used. Such argument are used, for example, in Gnome::CanvasLine.new and seems to set instance variables.

I didn't succeed to use such hash argument. The only way I got is by using {:@a => "a", :@b => "b"} with a code like

Class Foo
   def set(hash)
     hash.each {|k,v| self.instance_variable_set(k,v)}
   end
end

Can you explain me how {:a => "a", :b => "b"} should be handled?

Thanks

Nico

Hi --

···

On Sat, 5 Nov 2005, Nicolas Rassat wrote:

Hi

I'm doing some gnome/ruby stuff and I wonder how instance method's argument like {:a => "a", :b => "b"} can be used. Such argument are used, for example, in Gnome::CanvasLine.new and seems to set instance variables.

I didn't succeed to use such hash argument. The only way I got is by using {:@a => "a", :@b => "b"} with a code like

Class Foo
def set(hash)
   hash.each {|k,v| self.instance_variable_set(k,v)}
end
end

Can you explain me how {:a => "a", :b => "b"} should be handled?

instance_variable_set("@#{k}",v)

David

--
David A. Black
dblack@wobblini.net

David A. Black a écrit :

Hi --

snip

Class Foo
def set(hash)
   hash.each {|k,v| self.instance_variable_set(k,v)}
end
end

>>
snip

instance_variable_set("@#{k}",v)

David

Ahem... Looks it works

But it's almost 50% more times of computing. So why don't use the :@a syntax (yes in this case it's 50% more times writing... :wink:

Nico

···

On Sat, 5 Nov 2005, Nicolas Rassat wrote:

Quoting Nicolas Rassat <nicolas.rassat@free.fr>:

Ahem... Looks it works

But it's almost 50% more times of computing. So why don't use the
:@a
syntax (yes in this case it's 50% more times writing... :wink:

Well, it could be argued against on the grounds that using :@a
exposes internal implementation details.

-mental