Nosh
Nosh

Reputation: 493

Method definition without a name in Ruby on Rails

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?

Upvotes: 0

Views: 194

Answers (1)

Josh Lee
Josh Lee

Reputation: 177550

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.

Upvotes: 4

Related Questions