Skip to content

re: Reverted Ruby 2.7 new feature: Method reference operator VIEW POST

re: At first glance this seems a bit voodoo magic compared with the more explicit method, but the more I look at it, the more it does seem to be intuit...

: is used as a prefix in similar ways in Ruby.

Eh? : is never used as a prefix in Ruby. :: is—for namespacing in FQ names—but this is probably not as similar.

: as a prefix is used in Ruby only to declare symbols. If you think about [42].map(&:to_s) notation, there is no syntactic sugar. :to_s there is an old good plain symbol. It works because Symbol#to_proc is defined, no magic involved. & in arguments list calls to_proc on the argument and passes the result as a block to the calling method, e.g. symbol converted to a proc is passed to the receiver as a block parameter.

The naïve implementation of Symbol#to_proc in Ruby might be similar to the working implementation of the same for the string:

class String
  def to_proc
    ->(recv) { recv.send self }

#⇒ ["42"]
code of conduct - report abuse