Skip to content

Instantly share code, notes, and snippets.

@thomasbiddle
Last active December 13, 2015 17:39
Show Gist options
  • Save thomasbiddle/4949826 to your computer and use it in GitHub Desktop.
Save thomasbiddle/4949826 to your computer and use it in GitHub Desktop.
method_missing in ruby
module BADASS
class Config
def initialize(config_file)
@config_file = config_file
end
def method_missing(method, *args)
if method.to_s =~ /[a-zA-Z0-9\-_]=$/
puts method
puts args[0]
elsif method.to_s =~ /[a-zA-Z0-9\-_]/
puts method
else
super
end
end
end
end
myclass.username # returns from self.read(username)
myclass.username='bob' # calls self.write(username, 'bob')
myclass.username = 'bob' # still works! (Ruby ignores whitespace)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment