Created
May 8, 2014 08:41
-
-
Save vinhnglx/09a1dbe7df5ac6eeec64 to your computer and use it in GitHub Desktop.
An example about defining method_missing()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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