Created
October 21, 2011 10:29
-
-
Save moro/1303529 to your computer and use it in GitHub Desktop.
Symbol#<<
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Symbol | |
def <<(args) | |
lambda {|x| x.send(self, *args) } | |
end | |
end | |
p [{:a => 1}].find(&:key? << :a) # => {:a => 1} | |
p [{:a => 1}].find(&:key? << :b) # => nil | |
p [["abc", "efg"], ["foo", "bar"]].map(&:join << "-") # => ["abc-efg", "foo-bar"] | |
p ["abc", "efg", "foo", "bar"].map(&:sub << ["a", "A"]) # => ["Abc", "efg", "foo", "bAr"] |
SymbolはString的側面もあるので、Symbol#<<はちょっと自明さに欠ける気がします。
Procに部分適用メソッド(演算子)があってもいいかなとは思いますが、
結局、ふつうに書いてもいいかなという気がしてきました…
p [{:a => 1}].find(&->x{x.key?:a})
もっと普通に↓の方が短いような…
p [{:a => 1}].find{|x|x.key?:a}
{|x|x....} はなんかダブリ感があるんですが ->x{x...} は意外とそうでもないなと思った。なぜだろう
ああ、自分は { |x| x... } とスペースを入れる派だからだ!
->x { x.... } はけっこうボーダーラインww すんません個人的感覚の話でww
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
先客がいた...