custom attribute accessors ?

A

ako...

hello,

i need to define a number of custom attribute readers on my class. so
far i have come up with two ways to do it. the code below is a
simplified version. but i cannot decide which one is "preferable". whis
one would the experts use? why?

thanks
konstantin

class A
[:nameof].each { |x| module_eval "def #{x}() @#{x} end" }
def initialize() @nameof = self.class.to_s end
end

class B
[:nameof].each { |x| define_method(x) {
instance_variable_get("@#{x}") } }
def initialize() @nameof = self.class.to_s end
end
 
E

Eero Saynatkari

hello,

i need to define a number of custom attribute readers on my class. so
far i have come up with two ways to do it. the code below is a
simplified version. but i cannot decide which one is "preferable". whis
one would the experts use? why?

thanks
konstantin

class A
[:nameof].each { |x| module_eval "def #{x}() @#{x} end" }
def initialize() @nameof = self.class.to_s end
end

class B
[:nameof].each { |x| define_method(x) {
instance_variable_get("@#{x}") } }
def initialize() @nameof = self.class.to_s end
end

Hm, by your examples it would seem to be OK to
use the standard attr_* methods:

class A
# Symbol or String
[...].each {|attribute| attr_reader attribute}
end

Or did I incorrectly understand what you needed?


E
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,214
Latest member
JFrancisDavis

Latest Threads

Top