Tuesday, May 06, 2008

Ruby in LISP clothing

As it turns out, Ruby's syntax is somewhat flexible. As an example, parentheses are sometimes optional when calling a method.
puts "Oh hai!"
puts("Oh hai!")

link_to_remote "Go there", :url => { :controller => :there, :action => :go }
link_to_remote("Go there", { :url => { :controller => :there, :action => :go } })
The only time the parentheses are needed is when the invocation would otherwise be ambiguous:
link_to_remote h text, :url => { ... }
(h is the HTML escaping function. link_to_remote may actually automatically escape the HTML, but I'm not sure.)

Ruby can't tell what the user is trying to do. The normal way to fix this is to add some parentheses to the call:
link_to_remote(h(text), :url => { ... })
link_to_remote(h text, :url => { ... })
however, it is also possible to use LISP-like parentheses with the same result:
link_to_remote (h text), :url => {...}
(link_to_remote (h text), :url => {...})
Which is better? Certainly one of the first pair is more conventional. For some reason, though, I found myself migrating to the second pair. I suspect it's a combination of
  • I try to avoid using parentheses in method calls in Ruby as much as possible
  • LISP is awesome
  • I have done some Objective-C programming, in which you [object send:message]

1 comment:

Kyaw Kyaw Naing / George said...

http://ethicminds.blogspot.com/ some nice comments ob why BOTH Ruby and Lips are not READY.