What does send() do in Ruby?

后端 未结 6 1026
野性不改
野性不改 2020-11-28 01:49

Can someone please tell me what

send(\"#{Model.find...}\")

is and does?

6条回答
  •  囚心锁ツ
    2020-11-28 02:25

    Another example, similar to Antonio Jha's https://stackoverflow.com/a/26193804/1897857

    is if you need to read attributes on an object.

    For example, if you have an array of strings, if you try to iterate through them and call them on your object, it won't work.

    atts = ['name', 'description']
    @project = Project.first
    atts.each do |a|
      puts @project.a
    end
    # => NoMethodError: undefined method `a'
    

    However, you can send the strings to the object:

    atts = ['name', 'description']
    @project = Project.first
    atts.each do |a|
      puts @project.send(a)
    end
    # => Vandalay Project
    # => A very important project
    

提交回复
热议问题