Last active
March 29, 2017 02:36
-
-
Save aoitaku/5b363ee34fffcd426d0a to your computer and use it in GitHub Desktop.
Symbol#callでprocに変換して部分適用する
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 call(*argv) | |
case | |
when block_given? | |
-> obj { self.to_proc[obj, *argv, &proc] } | |
when argv.size > 0 | |
-> obj { self.to_proc[obj, *argv] } | |
else | |
self.to_proc | |
end | |
end | |
alias +@ to_proc | |
alias + call | |
def &(callable) | |
call(&callable) | |
end | |
end | |
p [[1,2,3],[2,4,6],[5,7,9]].map(&:inject.(:+)) | |
p [[1,2,3],[2,4,6],[5,7,9]].map(&:select&:odd?)) | |
p [[1,2,3],[2,4,6],[5,7,9]].map(&:slice.(1,1)) | |
# in case-when | |
case rand(10) | |
when +:zero? | |
p 'zero' | |
when :< + 3 | |
p 'under 3' | |
when 3 .. 5 | |
p 'between 3 and 5' | |
when :> + 5 | |
p 'over 5' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[]
をやめてcall
にしました。symbol.()
の形式で呼べます(うーねこさんありがとうございます)。