Skip to content

Instantly share code, notes, and snippets.

@bbugh
Created October 18, 2012 02:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bbugh/3909614 to your computer and use it in GitHub Desktop.
Save bbugh/3909614 to your computer and use it in GitHub Desktop.
Ruby 1.9 passing arguments to define_method blocks

In addition to Kevin Conner's answer: block arguments do not support the same semantics as method arguments. You cannot define default arguments or block arguments.

This is only fixed in Ruby 1.9 with the new alternative "stabby lambda" syntax which supports full method argument semantics.

http://stackoverflow.com/questions/89650/how-do-you-pass-arguments-to-define-method#answer-109379

Example:

# Works
def meth(default = :foo, *splat, &block) puts 'Bar'; end

# Doesn't work
define_method :meth { |default = :foo, *splat, &block| puts 'Bar' }

# This works in Ruby 1.9 (modulo typos, I don't actually have it installed)
define_method :meth, ->(default = :foo, *splat, &block) { puts 'Bar' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment