1

i'm so new to Ruby

we can say that attr_accessor :bark

is sugar coating for

 def bark
    @bark
  end

  def bark=(val)
    @bark = val
  end

What would be the same when given multiple parameters is it?

attr_accessor *args
    args.each { |attr|
     def attr
            @attr
          end

          def attr=(val)
            @attr= val
          end
    }

and if so how can i use the same with initialization of all given params also

and in class_eval(Meta OOP) with dynamic attr+random_str?

cypronmaya
  • 520
  • 1
  • 11
  • 24
  • 1
    I think you should have a look at http://stackoverflow.com/questions/752717/how-do-i-use-define-method-to-create-class-methods – pdu Mar 02 '12 at 12:10

1 Answers1

1

The stuff

attr_accessor *args
  args.each { |attr|
   def attr
     @attr
   end

   def attr=(val)
     @attr= val
   end
 }

won't work. It would create a method "attr" which returns the value of the instance variable "@attr". I think that's not what you want to do, right? You should do

args.each do |arg|
  self.define_singleton_method arg do self.instance_variable_get arg end
end

for the getter and almost the same stuff for the setter. I don't know what you mean with

and if so how can i use the same with initialization of all given params also and in class_eval(Meta OOP) with dynamic attr+random_str?

regards

musicmatze
  • 4,124
  • 7
  • 33
  • 48