Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Question about a multiple assignment statement used in Rails

Reply
Thread Tools

Question about a multiple assignment statement used in Rails

 
 
zenshade@gmail.com
Guest
Posts: n/a
 
      08-16-2006
As I'm very much still a ruby newbie, I'm hoping someone can help me to
correctly parse the following statement generated by Rails scaffolding:

@category_pages, @categories = paginate :category, er_page => 10

This statement occurs in the following context:

class CategoriesController < ApplicationController
..
..
..
def list
@category_pages, @categories = paginate :category, er_page => 10
end
..
..
..
end

Now, I know that paginate is a method of the ActionController class,
but is it being called with two arguments or one? And if it is being
called with two arguments (:category, er_page => 10), how is
multi-assignment working here?

To me, this looks like @category_pages will get the return value of the
paginate method, and @categories will be assigned the value nil.

Any advice on how to correctly understand this assignment statement
would be greatly appreciated.

 
Reply With Quote
 
 
 
 
Jan Svitok
Guest
Posts: n/a
 
      08-16-2006
On 8/16/06, <> wrote:
> As I'm very much still a ruby newbie, I'm hoping someone can help me to
> correctly parse the following statement generated by Rails scaffolding:
>
> @category_pages, @categories = paginate :category, er_page => 10
>
> This statement occurs in the following context:
>
> class CategoriesController < ApplicationController
> .
> .
> .
> def list
> @category_pages, @categories = paginate :category, er_page => 10
> end
> .
> .
> .
> end
>
> Now, I know that paginate is a method of the ActionController class,
> but is it being called with two arguments or one? And if it is being
> called with two arguments (:category, er_page => 10), how is
> multi-assignment working here?
>
> To me, this looks like @category_pages will get the return value of the
> paginate method, and @categories will be assigned the value nil.
>
> Any advice on how to correctly understand this assignment statement
> would be greatly appreciated.


let's simplify it:

var1, var2 = fnc :sym1, :sym2 => 10

1. the right-hand side: function's arguments are divided in two
groups: non-hash and hash. all the hash args are passed as one hash,
as last argument (also there can be a special argument &block, that
will take the block if any was passed).
So the call is equivalent to: fnc(:sym1, {:sym2 =>10})

2, now the left-hand side: if fnc returns an array (what I suppose is
the case), the array can be automatically split into items:

a, b = [1,2] #=> a == 1, b == 2

So that's it.

 
Reply With Quote
 
 
 
 
zenshade@gmail.com
Guest
Posts: n/a
 
      08-16-2006

Jan Svitok wrote:
> a, b = [1,2] => a == 1, b == 2
>
> So that's it.



Ah, I see. The method returns an object with multiple values that get
unpacked by the multi-assignment.

That's very cool, but a bit obtuse. I think I can learn to like it,
though .

Thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Evaluation order of assignment statement nachch@gmail.com C Programming 10 10-24-2006 12:16 AM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
assignment with *when* statement Valentin Tihomirov VHDL 16 11-04-2003 05:05 PM
Comparison of Bit Vectors in a Conditional Signal Assignment Statement Anand P Paralkar VHDL 2 08-04-2003 08:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57