anothermh
anothermh

Reputation: 10546

How does the method reference operator .: work?

Ruby 2.7.0-preview1 has introduced the method reference operator .: as an experimental feature. (more here and here).

There are some abstract examples available for how to use this new operator:

method = 42.:to_s
 => #<Method: Integer#to_s>
method.receiver
 => 42
method.name
 => :to_s
method.call
 => "42"

and:

method = File.:read
 => #<Method: File.read>
method.call('/Users/foo/.zshrc')
 => "export ZSH=$HOME/.zsh"

These abstract examples are not representative of real-world implementations. What is the plain-English explanation of the purpose and use of the method reference operator, defined in terms of practical and real-world examples?

Update

This question is not very useful because the method reference operator was removed from Ruby 2.7.0 before release. This question is left up for historical reasons.

Upvotes: 5

Views: 282

Answers (1)

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369468

The method reference operator .: is simply syntactic sugar for Object#method just like the function call operator .(). is simply syntactic sugar for #call.

Thus, the use cases for the method reference operator are the exact same ones as the use cases for the Object#method method … just with less keystrokes.

Upvotes: 5

Related Questions