3 ms·
While this is cool, you can also just do `some_enum.sum` (also works with string concacts) One thing that is cool about the .to_proc shorthand though is, for
by rubyist5eva 5y ago
While this is cool, you can also just do `some_enum.sum` (also works with string concacts)
One thing that is cool about the .to_proc shorthand though is, for example, you have a method
def square_it(x);
x * 2;
end;
people tend to write an enumeration that uses it like
items.map {|i| square_it i }
but you can actually write it like this
items.map(&method(:square_it))
So not only can you write shorthands to methods on the object itself, you can write shorthands to other methods and "automagically" pass the item as its argument. Nifty stuff.