Variables in ruby method names

前端 未结 4 1025
感情败类
感情败类 2021-01-30 00:50

I have the following code:

for attribute in site.device_attributes
  device.attribute
end

where I would like the code to substitute the value o

相关标签:
4条回答
  • 2021-01-30 01:27

    you can also do

    device.instance_eval(attribute)
    
    0 讨论(0)
  • 2021-01-30 01:34

    Matt and Maxim are both correct, but leave out a detail that might help you get your head around the #send syntax: In Ruby, calling a method is really sending a message. Softies on Rails has a relatively straightforward explanation of that.

    0 讨论(0)
  • 2021-01-30 01:44

    The "send" method should do what you're looking for:

    object = "upcase me!"
    method = "upcase"
    object.send(method.to_sym) # => "UPCASE ME!"
    
    0 讨论(0)
  • 2021-01-30 01:47

    You can use #send method to call object's method by method's name:

    object.send(:foo) # same as object.foo
    

    You can pass arguments with to invoked method:

    object.send(:foo, 1, "bar", 1.23) # same as object.foo(1, "bar", 1.23)
    

    So, if you have attribute name in variable "attribute" you can read object's attribute with

    object.send(attribute.to_sym)
    

    and write attribute's value with

    object.send("#{attribute}=".to_sym, value)
    

    In Ruby 1.8.6 #send method can execute any object's method regardless of its visibility (you can e.g. call private methods). This is subject to change in future versions of Ruby and you shouldn't rely on it. To execute private methods, use #instance_eval:

    object.instance_eval {
      # code as block, can reference variables in current scope
    }
    
    # or
    
    object.instance_eval <<-CODE
      # code as string, can generate any code text
    CODE
    

    Update

    You can use public_send to call methods with regard to visibility rules.

    object.public_send :public_foo # ok
    object.public_send :private_bar # exception
    
    0 讨论(0)
提交回复
热议问题