Getting object attributes

I want to do something similar to this:

class Foo
  @goo = 2
  @boo = 5
end

foo = Foo.new

["goo", "boo"].each do |attribute|
  puts foo.attribute # should print out 2 on the first loop, 5 on the
second loop
end

Ideas?

Hi --

I want to do something similar to this:

class Foo
  @goo = 2
  @boo = 5
end

foo = Foo.new

["goo", "boo"].each do |attribute|
  puts foo.attribute # should print out 2 on the first loop, 5 on the
second loop
end

Ideas?

Clarification requested:

Do you want instances of foo to be able to get access to the instance
variables of Foo itself? Or do you want instances of Foo (such as
foo) to have their own instances variables called @goo and @boo, and
reader methods to access them? You could do a bunch of different
variations on this but I'm not sure which scenario you're trying to
implement.

David

···

On Thu, 12 Aug 2004, Joe Laughlin wrote:

--
David A. Black
dblack@wobblini.net

Joe Laughlin wrote:

I want to do something similar to this:

class Foo
  @goo = 2
  @boo = 5
end

foo = Foo.new

["goo", "boo"].each do |attribute|
  puts foo.attribute # should print out 2 on the first loop, 5 on the
second loop
end

["@goo","@boo"].each do |attribute|
   puts foo.instance_variable_get attribute
end

Also:
foo.instance_variables.each do |attribute|
   puts foo.instance_variable_get attribute
   foo.instance_variable_set nil
end

Hope this helps.
Sam

Hi --

Joe Laughlin wrote:

> I want to do something similar to this:
>
> class Foo
> @goo = 2
> @boo = 5
> end
>
> foo = Foo.new
>
> ["goo", "boo"].each do |attribute|
> puts foo.attribute # should print out 2 on the first loop, 5 on the
> second loop
> end
["@goo","@boo"].each do |attribute|
   puts foo.instance_variable_get attribute
end

That will print:

  nil
  nil

@goo and @boo are instance variables of the Class object Foo, not of
its instances. You'd have to do:

  puts foo.class.instance_variable_get(attribute)

or, if you want to be direct:

  puts Foo.instance_variable_get(attribute)

This is one of those times where a Class object, despite being a
special thing in various ways, is behaving just like any old object.
In fact, it's better to respect that, and not access its instance
variables from outside unless it lets you -- which you could do
with:

  class Foo
    class << self
      attr_reader :goo, :boo
    end
    @goo = 2
    @boo = 5
  end

  puts Foo.goo # 2

Also:
foo.instance_variables.each do |attribute|
   puts foo.instance_variable_get attribute
   foo.instance_variable_set nil

(You need two arguments for instance_variable_set :slight_smile:

David

···

On Thu, 12 Aug 2004, Sam McCall wrote:

--
David A. Black
dblack@wobblini.net

David A. Black wrote:

Hi --

I want to do something similar to this:

class Foo
  @goo = 2
  @boo = 5
end

foo = Foo.new

["goo", "boo"].each do |attribute|
  puts foo.attribute # should print out 2 on the first
loop, 5 on the second loop
end

Ideas?

Clarification requested:

Do you want instances of foo to be able to get access to
the instance variables of Foo itself? Or do you want
instances of Foo (such as foo) to have their own
instances variables called @goo and @boo, and reader
methods to access them? You could do a bunch of
different variations on this but I'm not sure which
scenario you're trying to implement.

David

The second one, I believe.

···

On Thu, 12 Aug 2004, Joe Laughlin wrote:

David A. Black wrote:

That will print:

  nil

@goo and @boo are instance variables of the Class object Foo, not of
its instances.

Rule #1 of this newsgroup should be: "Read the question twice" and
Rule #2 should be "test your answer".
The rules should be automatically appended to every post :slight_smile:
I looked at that and assumed those definitions were in the initialize method. To be fair, I'm fairly sure that _was_ the intention :wink:

  foo.instance_variable_set nil

(You need two arguments for instance_variable_set :slight_smile:

comp.lang.ruby.instance_variable_set "@rule2" "test your answer"
Better?

Thanks for that, I think it means it's time for me to sleep.
Sam

Hi --

David A. Black wrote:
> Hi --
>
>
>> I want to do something similar to this:
>>
>> class Foo
>> @goo = 2
>> @boo = 5
>> end
>>
>> foo = Foo.new
>>
>> ["goo", "boo"].each do |attribute|
>> puts foo.attribute # should print out 2 on the first
>> loop, 5 on the second loop
>> end
>>
>>
>> Ideas?
>
> Clarification requested:
>
> Do you want instances of foo to be able to get access to
> the instance variables of Foo itself? Or do you want
> instances of Foo (such as foo) to have their own
> instances variables called @goo and @boo, and reader
> methods to access them? You could do a bunch of
> different variations on this but I'm not sure which
> scenario you're trying to implement.
>
>
> David

The second one, I believe.

Then what you want would probably be something like:

  class Foo
    attr_reader :goo, :boo
    def initialize
      @goo, @boo = 2, 5
    end
  end

  foo = Foo.new
  [:goo, :boo].each {|m| puts foo.send(m) }

David

···

On Fri, 13 Aug 2004, Joe Laughlin wrote:

> On Thu, 12 Aug 2004, Joe Laughlin wrote:

--
David A. Black
dblack@wobblini.net

Joe Laughlin wrote:

David A. Black wrote:

Hi --

I want to do something similar to this:

class Foo
@goo = 2
@boo = 5
end

foo = Foo.new

["goo", "boo"].each do |attribute|
puts foo.attribute # should print out 2 on the first
loop, 5 on the second loop
end

Ideas?
     

Clarification requested:

Do you want instances of foo to be able to get access to
the instance variables of Foo itself? Or do you want
instances of Foo (such as foo) to have their own
instances variables called @goo and @boo, and reader
methods to access them? You could do a bunch of
different variations on this but I'm not sure which
scenario you're trying to implement.

David
   
The second one, I believe.

then this will do what you want

class Foo
  attr_reader :goo, :boo
  def initialize()
    @goo = 2
    @boo = 5
  end
end

foo = Foo.new

["goo", "boo"].each do |attribute|
  puts foo.send(attribute) # should print out 2 on the first
loop, 5 on the second loop
end

@goo and @boo need to be initialised within a method or else they're instance variables of Foos metaclass rather than Foo itself.

···

On Thu, 12 Aug 2004, Joe Laughlin wrote:

--
Mark Sparshatt

Hi --

David A. Black wrote:
> That will print:
>
> nil
> nil
>
> @goo and @boo are instance variables of the Class object Foo, not of
> its instances.
Rule #1 of this newsgroup should be: "Read the question twice" and
Rule #2 should be "test your answer".
The rules should be automatically appended to every post :slight_smile:
I looked at that and assumed those definitions were in the initialize
method. To be fair, I'm fairly sure that _was_ the intention :wink:

I think it may have been... still waiting to hear from the OP on my
request for clarification :slight_smile:

>> foo.instance_variable_set nil
> (You need two arguments for instance_variable_set :slight_smile:
comp.lang.ruby.instance_variable_set "@rule2" "test your answer"
Better?

Yes -- you're only one comma away from having it parse :slight_smile:

David

···

On Thu, 12 Aug 2004, Sam McCall wrote:

--
David A. Black
dblack@wobblini.net