Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > What about {:a => "a", :b => "b"} argument

Reply
Thread Tools

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

 
 
Nicolas Rassat
Guest
Posts: n/a
 
      11-04-2005
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
 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      11-04-2005
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



 
Reply With Quote
 
 
 
 
Nicolas Rassat
Guest
Posts: n/a
 
      11-04-2005
David A. Black a écrit :
> Hi --
>
> On Sat, 5 Nov 2005, Nicolas Rassat wrote:
>
>>

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

Nico
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function argument dependent on another function argument? Reckoner Python 11 01-19-2009 03:31 AM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
defaulting argument to previous argument Bhushit Joshipura C++ 5 12-30-2003 03:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57