instance method Method#<<

self << callable -> Proc[permalink][rdoc][edit]

self と引数を合成した Proc を返します。

戻り値の Proc は可変長の引数を受け取ります。戻り値の Proc を呼び出すと、まず受け取った引数を callable に渡して呼び出し、その戻り値を self に渡して呼び出した結果を返します。

Method#>> とは呼び出しの順序が逆になります。

[PARAM] callable:
Proc、Method、もしくは任意の call メソッドを持ったオブジェクト。


def f(x)
  x * x
end

def g(x)
  x + x
end

# (3 + 3) * (3 + 3)
p (method(:f) << method(:g)).call(3) # => 36
call を定義したオブジェクトを渡す例

class WordScanner
  def self.call(str)
    str.scan(/\w+/)
  end
end

File.write('testfile', <<~TEXT)
  Hello, World!
  Hello, Ruby!
TEXT

pipeline = method(:pp) << WordScanner << File.method(:read)
pipeline.call('testfile') # => ["Hello", "World", "Hello", "Ruby"]

[SEE_ALSO] Proc#<<, Proc#>>