1
1

More than 5 years have passed since last update.

RubyでF#のパイプライン演算子みたいなことがしたい

Last updated at Posted at 2014-03-25

F#のパイプライン演算子が好きなので、Rubyでも似たことができたら良いなぁと思いやってみた。

pipeline.rb
module Pipeline
  def pipe
    if block_given?
      yield self
    else
      self
  end

  Object.send(:include, Pipeline)
end

なんか同じメソッドが既にどこかにありそうだけど気にしない。

ブログで書いたのと同じやつ

test.rb
#1234を二乗して、文字列にして、順番逆にして、数字に戻す
1234.pipe {|i| i ** 2}.pipe(&:to_s).pipe(&:reverse).pipe(&:to_i)
#=> 6572251

こういう使い方はあんまり嬉しくない。

糊付けするとか?

hoge.rb
hoge.pipe {|i| f(hoge) }.pipe {|i| g(i) }
hoge.rb
"hoge".pipe(&["a", "b", "c"].method(:join))
#=> "ahogebhogec"

使いどころが分からない。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1