ruby on rails - Chain list of calls on an object, then return the modified object -
i have rails object, namely order
. have list of calls need call on object: [:address, :number]
. chain these methods on given object, , return result. so, end value above equal call to: obj.address.number
.
currently, i've done using:
obj = order.first [:address, :name].each { |m| obj = obj.send(m) } puts obj
but feel there best way of achieving using more "ruby" approach.
you use ruby#inject achieve
result_object = [:address, :name].inject(order.first, :send) puts result_object
it analogous to
order.first.address.name
Comments
Post a Comment