getting object attributes

J

Joe Laughlin

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?
 
D

David A. Black

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
 
J

Joe Laughlin

David said:
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.
 
D

David A. Black

Hi --

David said:
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
 
M

mark sparshatt

Joe said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top