Still by VisualMode
Home Blogmarks TILs Consulting
Home Blogmarks TILs Consulting
← Back to all TILs

Passing Arbitrary Methods As Blocks

ruby Written July 29, 2015

Use Object#method to create a callable Method object that can be passed to methods that yield to a block.

def inc(x)
  x + 1
end

[1,2,3].map(&method(:inc))
#=> [2,3,4]

Other posts like this

  • Pass A Block To Count ruby
  • Forward All Arguments To Another Method ruby
  • Are They All True? ruby
  • Up And Down With Integers ruby
  • Refer To Implicit Block Argument With It ruby