Instance variable in method definition?

In this Padrino example:

MyApp.controller :products do
get :show, :map => “/this-is/:id/cool” do
@product = Product.find(params[:id])
render ‘products/show’

… I’m confused about the use of the symbol :products in the first
line and the instance variable @product in the 3rd line. Is :products a
parameter to the MyApp.controller method? If this is a method definition
how can an instance variable valid? I thought instance variables were
only valid within instances, ie. objects?



you are sending the method controller to the object MyApp.
So it’s not a method “definition” as you said.

Yes, it’s a parameter.

MyApp.controller(:products) do

would do the same.

This method “controller” is receiving a parameter and a block.
get is also a method calling.

get(:show, { :map => "/this-is/:id/cool }) do

is essentially the same (but uglier IMHO).

@product is an instance variable, yes.

It’s probably on the context of the MyApp object (unless the block
passed is being instance evaluated (on another instance)).

Abinoam Jr.

Sorry for the typo on this line

get(:show, { :map => “/this-is/:id/cool” }) do

Abinoam Jr.