5 ms·
I don't do any Ruby, but: can someone give a non-contrived situation where you would want to do this? What problem does this solve?
by scott_s 16y ago
I don't do any Ruby, but: can someone give a non-contrived situation where you would want to do this? What problem does this solve?
- jfmiller28 16y agoThe most sited example is ActiveRecord where finders can be auto-generated. For example `find_by_name_and_address` will be defined on the fly the first time it is used in code. Another common usage is a wrapper class that provides a unified interface to a set of underlying objects. If many of these objects have almost the same interface `def method_missing(method,args); @object.send(method, args);end` can save a lot of redundant definitions.
- draegtun 16y agoGood example would be XML/HTML builder. For eg in Ruby http://builder.rubyforge.org/ http://builder.rubyforge.org/ require 'rubygems' require_gem 'builder' builder = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2) builder.person { |b| b.name("Jim"); b.phone("555-1234") } # Prints: # <person> # <name>Jim</name> # <phone>555-1234</phone> # </person> Also see this recent HN thread: http://news.ycombinator.com/item?id=1626018 http://news.ycombinator.com/item?id=1626018
- scott_s 16y agoAnd why would you do this instead of just defining a print method that takes the XML tag and content as separate parameters?
- rue 16y agoBecause the way above is more natural.
- scott_s 16y agoNot to me; I find it too cute. The reason I call it such is that it uses a powerful mechanism to implement a trivial thing.
- draegtun 16y agoI find using this mechanism in builders like this is more appropriate and not an abuse. Its definitely aids development & maintenance. Conversely though I find that using it in ActiveRecord is cute (ie. a bad thing!).
- scott_s 16y agoWhy do you find it cute in ActiveRecord? (Recall that I don't develop with Ruby or Rails, so I'm genuinely curious.)
- draegtun 16y agoFirst off, I feel using mechanisms like method_missing should be used carefully. The Perl community abused things like AUTOLOAD & monkey-patching some years ago and learnt hard lessons and have now established good practises for their use. I feel Ruby/Rails world is going through the same growing pains but it does look like they are now maturing (eg. Rails 3). Now IMHO I see a builder as a good case for a method_missing mechanism (I think there were a few others mentioned in thread/post). No magic, just a simple one-to-one relationship between code and what it does (result): b.name "Jim" => <name>Jim</name> However I feel something like find_by_name_and_address in ActiveRecord is cute (both in nice and ugly sense!). There is so much more going on underneath an ORM that I'd be wary of the extra layers of complexity upon what is already an (acceptable) impedance mismatch. Bottom line, an ORM is important to me and I don't want any loose cogs lying around! I'd prefer to be explicit here: # using DBIx::Class my $p = $schema->resultset('Addresses')->find({ name => '...', address => '...' }); So while find_by_name_and_address is a pretty nice aesthetic its feels more plastic cosmetics compared the elegant cosmetics that Builder solutions provide. Disclaimer: I haven't touched Rails for about 4 years now. I don't actually know for certain that something like find_by_name_and_address did or still uses method_missing. It could actually build methods like these straight from schema introspection. I would guess method_missing was used to avoid a performance hit on building all these at startup? If so then this could be a good argument for using method_missing here!
- draegtun 16y agoPurely for aesthetic reasons. Instead of: builder.person do |b| b.name("Jim") b.phone("555-1234") end You could write it like this: builder.tag!('person') do |b| b.tag!('name', "Jim") b.tag!('phone', "555-1234") end
- erikpukinskis 16y agoNot sure if Builder does this, but in theory you could even do: builder.person do |b| b.name = "Jim" b.phone = "555-1234" end
- draegtun 16y agoNot sure myself but it can also be written like this: builder.person do |b| b.name "Jim" b.phone "555-1234" end
- scott_s 16y agoRight, which is what I'm trying to understand: what does this allow you to do that is not just a cute way to call a method? I think the ActiveRecord example below is the answer I'm looking for, since it creates a method at runtime based on information that did not exist until runtime. But I need to read up on it to understand it.
- draegtun 16y agoPersonally I find aesthetics an important part of programming. If it looks good to me then it often means I'm doing something right! So I don't view it has cute but more it being succinct.
- scott_s 16y agoI also find aesthetics important, but I don't find this more aesthetically pleasing; it pings the "something's wrong" feeling. Being succinct is a goal, but it's not the only goal.