Skip to content

Instantly share code, notes, and snippets.

@timothyw
Created February 9, 2010 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timothyw/299551 to your computer and use it in GitHub Desktop.
Save timothyw/299551 to your computer and use it in GitHub Desktop.
# Using optional arguments, with a default value for "two"
define_method :method_name do |required, *optional|
# one, two, = *optional
one, two, *ignored = *optional
# two will have a default value
two ||= "Default value"
# ...
end
# With an options hash, with default values for the options
define_method :method_name do |required, *options|
default_options = { :foobar => "default value }
options = (options.first || Hash.new).merge(default_options)
foobar = options[:foobar]
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment