instance method Proc#<<

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

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

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

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

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


f = proc { |x| x * x }
g = proc { |x| x + x }

# (3 + 3) * (3 + 3)
p (f << 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 = proc { |data| puts "word count: #{data.size}" } << WordScanner << File.method(:read)
pipeline.call('testfile') # => word count: 4

[SEE_ALSO] Method#<<, Method#>>