Search

Enter a search word or two and press return to see the search results.

Who am I?

Hi, I’m Graeme and these are my notes, from my messy desk. I started this blog because Google proved to be more useful at finding content than anything else I’ve used.

So I started adding my own content in the hopes that Google would index it and allow me to find things again in the future.

It works.

You can find out more about me here, and you should follow me on Twitter here.

Keeping up

You can automatically receive new content here by subscribing to the “Blog RSS” (link below). This is the easiest way to keep up with what I write here.  See this BBC article for a good introduction on RSS and keeping up with the goings on of the Internet more easily.

« History meme | Main | Some new git techniques for your arsenal »
Tuesday
15Apr2008

Objective-C message passing semantics for Ruby

While I was writing a bit of code today (which smells a bit and so I suspect needs revisiting in the future, but anyway) I found myself writing the equivalent of:

object.respond_to?(method) ? object.send(method, args) : nil

that is, if a particular object implements a method then please call it and let me know what it returns, otherwise just pretend I never asked and return `nil`. While mulling over the `git diff`, it occurred to me that is (if I remember correctly) exactly how Objective-C behaves by default with message passing.

And so could Ruby:

Kernel.module_eval do
def method_missing(*args, &block)
nil
end
end

I am in no way advocating this as not being insane (a number of projects rely on the existing default `method_missing` implementation which raises an exception), but it is possible. :-)

Let's see it in action in irb. Before:

irb(main):001:0> o = Object.new
=> #
irb(main):002:0> o.method_that_does_not_exist
NoMethodError: undefined method `method_that_does_not_exist' for #
from /Users/mathie/.irbrc:210:in `method_missing'
from (irb):2

And after:

irb(main):008:0> o = Object.new
=> #
irb(main):009:0> o.method_that_does_not_exist
=> nil

Could be useful if Objective-C is your hammer and you find yourself trying to use Ruby as a hammer instead?

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (4)

This reminds me a lot of a post I made about the ? operator in Io -- remarkably similar issue; http://codefluency.com/articles/2006/12/16/that-nifty-question-mark

April 15, 2008 | Unregistered CommenterBruce

This is just stupid. Good luck searching bugs with this code.

April 15, 2008 | Unregistered CommenterMatthijs Langenberg

Instead of going ape-crazy and making it affect all method calls, why not just go:

class Object
def gingerly method, *args
respond_to? method ? send(method, args) : nil
end
end

This way, every object you now have can be asked to do something gingerly:

muffin.gingerly :wash, :with => :soap # => nil
"abc".gingerly :kiss #=> nil
caramel + 5 #=> nil
5 + 5#=> 10

You wont affect pre-existing methods and modules, but you still get delicious convenience. Or even:

class BashfulMaiden
def initialize(obj)
@obj = obj
end
def method_missing(method, *args)
@obj.respond_to? method ? @obj.call(method,args) : nil
end
end

And then:

class Object
def bashfully
BashfulMaiden.new(self)
end
end

This way, you won't pollute method missing for everyone, and you can do regular calls without having to do symbols, like so:

muffin.bashfully.wash 12
icecream.bashfully.lick
5.bashfully + 5

And so forth. By all means, Ruby is an expressive wonderland of pleasure, but just with other expressive wonderlands of pleasure, you want to use protection so you don't make a mess.

April 16, 2008 | Unregistered CommenterLeon

Just to be clear; I wasn't advocating actually doing this, just noting that it was possible. :-) If you actually want semantics like this, then @Leon's comment is a sane way to get it. The key thing it does is that it makes it explicit to the reader of the code that you're wanting those semantics. My insane version will trip up any other programmer that touches your application!

April 16, 2008 | Unregistered CommenterGraeme Mathieson

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>