Question

I want to do the following:

 object.method1.method2.method3.method4

except that all methods are in one array. So, in an example:

 object = 1
 methods = %W(to_s split shift)
 # should somehow do 1.to_s.split.shift

How can I call the methods in sequence on object?

Était-ce utile?

La solution

Do as below using Enumerable#inject

object = 1
%w(to_s split shift).inject(object,:send) # => "1"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top