On Tue, 19 Jul 2005, Casper wrote:

> Devin Mullins wrote:
>> Casper wrote:
>>
>>> 1.   class MyController < ActionController::Base
>>> 2.     helper :my_helper
>>> 3.   end
>>>
>>> I see this construct in Ruby on Rails a lot, and I don't know what is
>>> going on in line 2.
>>>
>>> Is this invoking a method called "helper" with the argument
>>> ":my_helper"? And if so, when does this get invoked?
>>>
>>> Thanks.
>>>
>>>
>> You are on the money. I will attempt to explain, but you should read the
>> PickAxe for the full Ruby goodness.
>>
> <snip>
>
> Thanks for the detailed explanation.
>
> I guess two reasons that I had difficult figuring out are (1) Ruby
> allows executable stuff to go where I expect to see declarations, but
> moreover (2) Ruby allows method calls to have their arguments
> delineated with or without parentheses. In particular, I don't
> understand the why you would want this. Is there a good motivation to
> allow it?

the best reason is the example you posted - one can make methods which read
like syntax additions/declarations.  the built-in

   class C
     attr_accessor 'a'
   end

which (just in case you didn't know) automatically generate accessor methods
for C objects as in

   obj = C::new
   obj.a = 42
   p obj.a      #=> 42

and this reads really nicely.  another good reason to ignore parens is for
boolean methods like

   if array.emtpy?
     array << 42
   end

i think most would agree that this looks better than

   if array.emtpy?()

and of course

   array << 42

looks a heck of a lot better than

   array.<<(42)

remember, even '<<' is a method in ruby!  then there are always methods like

   element = array.pop

which just reads quite nicely without parens and last, but not least, those of
us who've detest any extra chars in code like '$', ';', and even parens.  they
don't call it 'poetry' mode for nothing ;-)

cheers.

-a
-- 
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| My religion is very simple.  My religion is kindness.
| --Tenzin Gyatso
===============================================================================