« Back to Index

Example script that uses the & trick (typically used to convert an object to a proc; e.g. https://gist.github.com/Integralist/11206577) to convert a method (retrieved using method()) to a proc so it can be utilised by another method

View original Gist on GitHub

ruby-&-method-to_proc.rb

def hello(&block)
  block.call if block_given?
end

def world
  "world"
end

hello                  # => nil
hello { "world" }      # => "world"
hello(&method(:world)) # => "world"