0

I've been playing with MacRuby, and noticing how it extends Ruby to be able to handle the Smalltalk-like method (or message) signatures of Objective-C. At first glance, I thought that it looked a lot like the new Keyword Arguments of Ruby 2.0, but further inspection shows that they work in a fundamentally different way.

My first clue was when reading the MacRuby method spec on GitHub.

it "can have multiple arguments with the same name" do
  def @o.doSomething(x, withObject:y, withObject:z); x + y + z; end

  @o.should have_method(:'doSomething:withObject:withObject:')
  @o.should_not have_method(:'doSomething')
end

As far as I know, that behavior is not allowed in Ruby 2.0, because the withObject: part would be used as the sole identifier for the parameter, and therefore there could not be two with the same name.

Is this an insurmountable problem? Will MacRuby be forced to remain with Ruby 1.9 because of this?

Michael Dorst
  • 8,210
  • 11
  • 44
  • 71

1 Answers1

1

The key difference between keyword arguments and interleaved arguments is exactly as you've guessed; the keywords are not part of the method name (the selector in Objective-C).

Specifically, you can't reorder or drop parts of a Objective-C method's selector because that would be naming a different method.

This is also why us Obj-C graybeards bristle whenever someone describes Objective-C methods as having keywords preceding each argument.

You might find the question and answers on this particular question relevant. Brad Cox -- one of the inventors of Objective-C -- answered.

Community
  • 1
  • 1
bbum
  • 162,346
  • 23
  • 271
  • 359