Named Arguments

Discussion in 'Ruby' started by Kevin Olbrich, Aug 23, 2005.

  1. I was working through the new version of the pickaxe tonight and decided to
    try the 'named arguments' feature. The book suggests that one can simply
    pass a hash with the names of local variables and they with automagically
    get set to the hash values.

    I was not able to get this to work. Am I missing something here, or is this
    feature not working as described?

    _Kevin
     
    Kevin Olbrich, Aug 23, 2005
    #1
    1. Advertising

  2. Kevin Olbrich

    Brian Takita Guest

    ------=_Part_825_25094961.1124771547399
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Here is how you can set the instance variable @test

    class Test
    attr_accessor :test

    def initialize(args)
    @test =3D args[:test] unless args[:test].nil?
    end
    end

    t =3D Test.new({:test=3D>'test one'})
    p t.test


    This way is more generic way to set the instance variables:

    class TestGeneric
    attr_accessor :test, :another_test

    def initialize(args)
    args.each do |k, v|
    self.instance_variable_set("@#{k.to_s}", v)
    end
    end
    end

    t =3D TestGeneric.new({:test=3D>'test two', :another_test=3D>'test two agai=
    n'})
    p t.test
    p t.another_test


    Of course, there may be a better way to do this.

    :Brian

    On 8/22/05, Kevin Olbrich <> wrote:
    >=20
    > I was working through the new version of the pickaxe tonight and decided=

    =20
    > to
    > try the 'named arguments' feature. The book suggests that one can simply
    > pass a hash with the names of local variables and they with automagically
    > get set to the hash values.
    >=20
    > I was not able to get this to work. Am I missing something here, or is=20
    > this
    > feature not working as described?
    >=20
    > _Kevin
    >=20
    >=20
    >


    ------=_Part_825_25094961.1124771547399--
     
    Brian Takita, Aug 23, 2005
    #2
    1. Advertising

  3. Kevin Olbrich

    Brian Takita Guest

    ------=_Part_877_12706394.1124774088008
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Here is yet another way (
    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/52331) to do=20
    this:

    class AnotherTest
    attr_accessor :test, :another_test

    def initialize(args)
    @test, @another_test =3D args.values_at:)test, :another_test)
    end
    end

    t =3D AnotherTest.new({:test=3D>'test two', :another_test=3D>'test two agai=
    n'})
    p t.test
    p t.another_test


    On 8/22/05, Brian Takita <> wrote:
    >=20
    > Here is how you can set the instance variable @test
    >=20
    > class Test
    > attr_accessor :test
    >=20
    > def initialize(args)
    > @test =3D args[:test] unless args[:test].nil?
    > end
    > end
    >=20
    > t =3D Test.new({:test=3D>'test one'})
    > p t.test
    >=20
    >=20
    > This way is more generic way to set the instance variables:
    >=20
    > class TestGeneric
    > attr_accessor :test, :another_test
    >=20
    > def initialize(args)
    > args.each do |k, v|
    > self.instance_variable_set("@#{k.to_s}", v)
    > end
    > end
    > end
    >=20
    > t =3D TestGeneric.new({:test=3D>'test two', :another_test=3D>'test two ag=

    ain'})
    > p t.test
    > p t.another_test
    >=20
    >=20
    > Of course, there may be a better way to do this.
    >=20
    > :Brian
    >=20
    > On 8/22/05, Kevin Olbrich <> wrote:
    > >
    > > I was working through the new version of the pickaxe tonight and decide=

    d
    > > to
    > > try the 'named arguments' feature. The book suggests that one can simpl=

    y
    > > pass a hash with the names of local variables and they with=20

    > automagically
    > > get set to the hash values.
    > >
    > > I was not able to get this to work. Am I missing something here, or is
    > > this
    > > feature not working as described?
    > >
    > > _Kevin
    > >
    > >
    > >

    >=20
    >


    ------=_Part_877_12706394.1124774088008--
     
    Brian Takita, Aug 23, 2005
    #3
  4. Brian Takita wrote:
    > Here is yet another way (
    > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/52331) to
    > do this:
    >
    > class AnotherTest
    > attr_accessor :test, :another_test
    >
    > def initialize(args)
    > @test, @another_test = args.values_at:)test, :another_test)
    > end
    > end
    >
    > t = AnotherTest.new({:test=>'test two', :another_test=>'test two
    > again'}) p t.test
    > p t.another_test


    Note that you don't need the curly braces, which makes stuff look a bit
    better:

    t = AnotherTest.new:)test=>'test two', :another_test=>'test two')

    Kind regards

    robert
     
    Robert Klemme, Aug 23, 2005
    #4
    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. Fuzzyman
    Replies:
    2
    Views:
    312
    Fuzzyman
    Feb 22, 2005
  2. Ramashish Baranwal

    Passing variable number of named arguments

    Ramashish Baranwal, Dec 27, 2006, in forum: Python
    Replies:
    2
    Views:
    280
    Ramashish Baranwal
    Dec 28, 2006
  3. Andrey Vul
    Replies:
    5
    Views:
    658
    Saeed Amrollahi
    Nov 4, 2009
  4. Tim Chase
    Replies:
    2
    Views:
    136
    Tim Chase
    Oct 9, 2011
  5. Roger Pack
    Replies:
    2
    Views:
    115
    Roger Pack
    Jan 4, 2010
Loading...

Share This Page