How to specify method default argument using define_method?

This actually just works like you would expect in Ruby 1.9!

define_method :m do |a = false|
end

If you need 1.8 compatibility, but you don’t necessarily need a closure to define your method with, consider using class_eval with a string argument and a regular call to def:

class_eval <<-EVAL
  def #{"m"}(a = false)
  end
EVAL

Otherwise follow the suggestion in the thread that philippe linked to. Example:

define_method :m do |*args|
  a = args.first
end

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)