Custom attribute accessors?

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

···

On 2006.01.07 17:13, ako... wrote:

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