Skip to content

Instantly share code, notes, and snippets.

@vinhnglx
Created May 8, 2014 08:41
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 vinhnglx/09a1dbe7df5ac6eeec64 to your computer and use it in GitHub Desktop.
Save vinhnglx/09a1dbe7df5ac6eeec64 to your computer and use it in GitHub Desktop.
An example about defining method_missing()
class User
attr_accessor :first_name, :last_name, :birthday
# method_missing() is passed 2 arguments: name of the missing method and arrays of its arguments
# reference at the link: http://www.ruby-doc.org/core-2.1.0/BasicObject.html#method-i-method_missing
def method_missing(name, *arg)
puts "#{name} was called with arguments: #{arg.join(',')}. #{name} method not available."
end
end
# Results
# user = User.new
# user.first_name => nil
# user.check_last_name("Nguyen", "Le") => check_last_name was called with arguments: Nguyen, Le. check_last_name method not available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment