Do as below using Enumerable#inject
object = 1
%w(to_s split shift).inject(object,:send) # => "1"
Pergunta
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?
Solução
Do as below using Enumerable#inject
object = 1
%w(to_s split shift).inject(object,:send) # => "1"