Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: adding instance variables to an object

Tags:

ruby

How can I add a bunch of instance variables from one object to another?

For example, imagine robots where you have the base robot and you can customize it with add-ons.

class Robot

   def initialize
      @name = "simple robot"
      @power = nil #no power
      @speed = nil
      # more attributes
   end

   def add_attributes(addon)
      @power = addon.power
      @speed = addon.speed
      #the rest of the attributes that addon has
   end
end

I would like to re-write the add_attributes method to simply iterate over each of the addon's attributes instead of writing them all one by one, cause there could be dozens of attributes.

Some addons may have instance variables that Robot doesn't have, and I would also like to add them to Robot. Like creating instance variables on the fly?

like image 452
MxLDevs Avatar asked Dec 09 '22 00:12

MxLDevs


2 Answers

It depends on what you mean by "attribute"; Ruby doesn't have that concept directly but you could copy instance variables from one object to another as such:

def add_attributes(addon)
  addon.instance_variables.each do |x|
    self.instance_variable_set(addon.instance_variable_get(x))
  end      
end

[Edit] Note that the answer by @HolgerJust is also a good solution.

like image 156
maerics Avatar answered Dec 23 '22 10:12

maerics


You could get rid of the instance variables and use a single hash instead. This has the advantage of a free enumerator and a clean interface to access all the features a robot has from one convenient place.

It also avoids having to mess with an instances internal variables. They are typically intended to be internal and are used for a vast amount of stuff. If you want to expose functionality, you should do it with a public method. Messing with internal state is at least bad design and will most probably lead to much grief later on. Generally, it's best to avoid meta-programming wherever possible.

class Robot
  attr_reader :features

  def initialize
    @features = {}
    @features[:name] = "simple robot"
    @features[:power] = nil #no power
    @features[:speed] = nil
  end

  def add_attributes(addon)
    @features.merge! addon.features
  end
end
like image 23
Holger Just Avatar answered Dec 23 '22 09:12

Holger Just