Created
May 13, 2012 00:28
-
-
Save sahglie/2669899 to your computer and use it in GitHub Desktop.
Making ruby act like python
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 TestObject | |
def method_missing(message, *args, &block) | |
if !self.respond_to?(message) && message[/=/] | |
self.class.send(:attr_accessor, message[0...-1]) | |
self.send(message, args[0]) | |
else | |
super | |
end | |
end | |
end | |
o = TestObject.new() | |
begin | |
x = o.bar() | |
puts x | |
rescue NoMethodError | |
puts "No method 'bar'" | |
end | |
o.bar = "array of objects" | |
x = o.bar() | |
puts x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment