Skip to content

Instantly share code, notes, and snippets.

@hanachin
Last active May 6, 2019 09:11
Show Gist options
  • Save hanachin/2060751fca3444922386879ac666541e to your computer and use it in GitHub Desktop.
Save hanachin/2060751fca3444922386879ac666541e to your computer and use it in GitHub Desktop.
Ruby 2.7の新機能メソッド参照演算子 ref: https://qiita.com/hanachin_/items/1aa1ba38a87dee91aed6
123.method(:to_s)
# => #<Method: Integer#to_s>
meth = 123.method(:to_s)
# => #<Method: Integer#to_s>
meth.call
# => "123"
meth.call(16)
# => "7b"
123.:to_s
# => #<Method: Integer#to_s>
class Object
def method(*)
nil
end
end
123.method(:to_s)
# => nil
123.:to_s.call
# => "123"
using Module.new {
refine(Object) do
def method(*); end
end
}
method(:puts)
# => nil
self.:puts
# => #<Method: main.puts>
using Module.new {
refine(Object) do
def method(*); end
end
}
self.:method.call(:puts).call(1)
# 1
1.method(:-@).call
# => -1
1.:[email protected]
# => -1
[1,2,3].map(&2.:*)
# => [2, 4, 6]
["エロ"].any?(&"イエロー".:include?)
# => true
require "prime"
(1..10).select(&Prime.:prime?)
# => [2, 3, 5, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment