views:

55

answers:

1

I recently looked at sample code for a controller in a rails project which included a method definition in a class without defining the name of the method like so:

def begin
  redirect_to :action => :buy, :PaymentAction => params[:paymentaction]
rescue Errno::ENOENT => exception
  flash[:error] = exception
  redirect_to :controller => 'wppro', :action => 'exception'  
end

Is this a way of defining a constructor in rails?

+4  A: 

In this case, begin is just the name of a method; it's unrelated to the beginrescue syntax for handling exceptions (in which the begin is sometimes optional). foo.begin is valid syntax for calling this method as well.

Since we're inside a Rails controller, begin is additionally the name of an action.

Constructors are defined with the initialize instance method.

jleedev