3 ms·
It is my impression that Python does exactly the things listed here, but more explicitly. With the python approach, instance variables are public by default (as
by akoumjian 13y ago
It is my impression that Python does exactly the things listed here, but more explicitly. With the python approach, instance variables are public by default (as attributes). There is never any confusion between when you are calling a method, or referencing an attribute. If you are doing something something more complicated than getting or setting an attribute, you must either call a method (this is so you and other developers actually know what is going on), or alternatively you use a property decorator.
You could do something similar to Ruby's missing_method, by setting the class's __getattr__, but you'd really have to ask yourself why. What kind of creation are you building that you are expecting to not know which methods are available while running your program?
These are the kinds of things that drive me batty with Ruby. Perhaps if I spent about double or triple the hours on Ruby that I have, I might have an "aha" moment. However, from what I've seen I can't help but feel that what it's really trying to accomplish is much more comfortable in functional form (say Lisp?).
- area51org 13y agoWhat kind of creation are you building that you are expecting to not know which methods are available while running your program? Well, metaprogramming is all about exactly that kind of thing. Python is a fine language, but it's not cut from the same cloth as Ruby. That's not a value judgement; plenty of sane, creative, and intelligent people use Python. They're simply not the same kind of language.
- acjohnson55 13y agoYou can do this sort of metaprogramming in Python just fine, and then some. Things like metaclasses and abstract base classes give a lot of power to hook and modify the type system.
- area51org 13y agoYes, that all may be true, but white space has semantic meaning in Python. I just can't get past that. ;-)
- dpritchett 13y agoTry listening to Sandi Metz on the Ruby Rogues podcast. It's a longish episode but she'll give you an appreciation for message-passing OO and how it makes an application design more pliable. In a pinch the (free?) "Barewords" episode of Ruby Tapas will get you close in five minutes.
- akoumjian 13y agoAwesome, will do.
- edmundhuber 13y agoSomething I feel like I frequently read in these discussions is that Ruby is nice "because it looks almost like English". When I think of people speaking English, I think of people equating quantity with quality, exchanging clarity for context. This is why I vastly prefer Python even though they are really not that dissimilar.
- griffindy 13y ago> What kind of creation are you building that you are expecting > to not know which methods are available while running your program? `method_missing` is more useful for dynamically responding to method calls than actually not knowing how to respond. A good example of this is rails' (version 3.2 and lower) dynamic finders: User.find_by_username('a_username') `find_by_username` is never defined anywhere, it is dynamically responded to using method missing. this allows code to be much more precise and DRY (though at the expense of readability if you don't know what's going on)
- jfarmer 13y ago"What kind of creation are you building that you are expecting to not know which methods are available while running your program?" You normally wouldn't use method_missing in this way. As an example, for better or worse, look at RSpec. I can write code like this: require 'rspec' class Person def initialize @happy = true end def happy? @happy end end describe Person do describe '.new' do it 'is happy by default' do expect(Person.new).to be_happy end end end RSpec doesn't know about the Person#happy? method, obviously, so how the heck is there a be_happy method? It's using method_missing to intercept the call to the otherwise-undefined be_happy method, pull out the "happy" part, translate it to "happy?", and then construct the appropriate expectation by delegating the "happy?" message to the underlying Person object and analyzing the response. It also enables RSpec to generate much more useful testing messages. Instead of saying "Expected person.happy? to be true, but got false" it can say "Expected person to be happy, but happy? returned false." This intercept-and-delegate or intercept-and-translate mechanism is probably the most common use of method_missing. It makes writing DSLs much easier. See, e.g., Nokogiri::XML::Builder for generating XML documents (http://nokogiri.org/Nokogiri/XML/Builder.html http://nokogiri.org/Nokogiri/XML/Builder.html).