开发者

Ruby: Is it possible to set the value of a instance variable where the instance variable is named via a string?

开发者 https://www.devze.com 2023-01-28 22:49 出处:网络
Not sure what this pattern is called, but here is the scenario: class Some #this class has instance variables called @thing_1, @thing_2 etc.

Not sure what this pattern is called, but here is the scenario:

class Some
   #this class has instance variables called @thing_1, @thing_2 etc.
end

Is there some way to set the value of the instance variable where the instance variable name i开发者_JAVA百科s created by a string?

Something like:

i=2
some.('thing_'+i) = 55 #sets the value of some.thing_2 to 55


Search for “instance_variable” on Object:

some.instance_variable_get(("@thing_%d" % 2).to_sym)
some.instance_variable_set(:@thing_2, 55)

This pattern is referred to as “fondling”; it can be a better idea to explicitly use a Hash or Array if you will be computing keys like this.


You can generate accessor methods for those instance variables and then just send setters:

class Stuff
  attr_accessor :thing_1, :thing_2
end

i = 1
s = Stuff.new
s.send("thing_#{i}=", :bar)
s.thing_1 # should return :bar


I was wondering the same, so I've been playing with the console. Fun stuff :

class Parent
  class << self
    attr_accessor :something

    def something(value = nil)
      @something = value ? value : @something
    end

    def inherited(subclass)
      self.instance_variables.each do |var|
        subclass.instance_variable_set(var, self.instance_variable_get(var))
      end
    end
  end

  attr_accessor :something
  self.something = 'Parent Default'

  def something(value = nil)
    @something = value ? value : @something ? @something : self.class.something
  end
end

class Child < Parent
  # inherited form Parent.something
end

class GrandChild < Child
  something "GrandChild default"
end

Results in :

Parent.something
# => "Parent Default" 
Parent.something = "Parent something else"
# => "Parent something else" 
Parent.something
# => "Parent something else" 
parent = Parent.new
# => #<Parent:0x007fc593474900> 
parent.something
# => "Parent something else" 
parent.something = "yet something different"
# => "yet something different" 
parent.something
# => "yet something different" 
parent.class.something
# => "Parent something else" 
Child.something
# => "Parent Default" 
child = Child.new
# => #<Child:0x007fc5934241f8> 
child.something
# => "Parent Default" 
GrandChild.something
# => "GrandChild default" 
GrandChild.something("grandchild something else") 
# => "grandchild something else" 
GrandChild.something
# => "grandchild something else" 
GrandChild.superclass.something
# => "Parent Default" 
grandchild = GrandChild.new
# => #<GrandChild:0x007fc5933e70c8> 
grandchild.something
# => "grandchild something else" 
grandchild.something = "whatever"
# => "whatever" 
GrandChild.something
# => "grandchild something else" 
grandchild.something
# => "whatever"
0

精彩评论

暂无评论...
验证码 换一张
取 消