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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -