re: Ruby 2.6 new feature: refine `#to_proc` VIEW POST

TOP OF THREAD FULL DISCUSSION
re: My guess is &1 is 1.to_proc() and o is the receiver object (e.g. a.map(&0) would do a.map( -> a[0])) and the sugar diabetes thingy is a....
 

That's correct!

& would implicitly call #to_proc to convert the object to Proc object.

Any object that implements the to_proc method can be converted into a proc by the & operator, and therefore con be consumed by iterators.
docs.ruby-lang.org/en/2.6.0/Proc.h...

But before 2.6, #to_proc that defined in Refinements are ignored at & operator.

# foo.rb
using Module.new {
  refine(Integer) do
    def to_proc
      -> o { o[self] }
    end
  end
}

p 1.then(&0)
% ruby -v foo.rb
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin17]
Traceback (most recent call last):
foo.rb:9:in `<main>': wrong argument type Integer (expected Proc) (TypeError)

In 2.6, we can use #to_proc for & operator.

% ruby -v foo.rb
ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin17]
1
code of conduct - report abuse