Domanda

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?

È stato utile?

Soluzione

Do as below using Enumerable#inject

object = 1
%w(to_s split shift).inject(object,:send) # => "1"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top