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

Discussion in 'Ruby' started by Nicolas Rassat, Nov 4, 2005.

  1. 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 {:mad:a => "a", :mad: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
     
    Nicolas Rassat, Nov 4, 2005
    #1
    1. Advertising

  2. 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
    > {:mad:a => "a", :mad: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
     
    David A. Black, Nov 4, 2005
    #2
    1. Advertising

  3. 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 :mad:a
    syntax (yes in this case it's 50% more times writing... ;-)

    Nico
     
    Nicolas Rassat, Nov 4, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    421
  2. Ben Kial
    Replies:
    1
    Views:
    700
    Eric Enright
    Nov 15, 2004
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,963
    Eric Sosman
    Jan 7, 2005
  4. nw
    Replies:
    0
    Views:
    334
  5. Reckoner
    Replies:
    11
    Views:
    726
    Steven D'Aprano
    Jan 19, 2009
Loading...

Share This Page