------ art_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
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
On 8/22/05, Brian Takita <brian.takita / gmail.com> wrote:
>
> Here is how you can set the instance variable @test
>
> class Test
> attr_accessor :test
>
> def initialize(args)
> @test = args[:test] unless args[:test].nil?
> end
> end
>
> t = Test.new({:test=>'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 = TestGeneric.new({:test=>'test two', :another_test=>'test two again'})
> 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 <kevin.olbrich / duke.edu> wrote:
> >
> > 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
> >
> >
> >
>
>
------ art_877_12706394.1124774088008--