문제

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?

도움이 되었습니까?

해결책

Do as below using Enumerable#inject

object = 1
%w(to_s split shift).inject(object,:send) # => "1"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top