What is the ruby equivalent of python's getattr

前端 未结 4 1123
慢半拍i
慢半拍i 2020-12-10 14:05

I am new to rails and trying to do a little refactoring (putting a partial renderer that lists titles in app/views/shared ) The renderer shows the dates along with the title

4条回答
  •  自闭症患者
    2020-12-10 15:03

    I think the answer to the original question is send:

    irb(main):009:0> t = Time.new
    => Thu Jul 02 11:03:04 -0500 2009
    irb(main):010:0> t.send('year')
    => 2009
    

    send allows you to dynamically call an arbitrarily-named method on the object.

提交回复
热议问题