[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

<< == push?

Giles Bowkett

11/28/2006 8:17:00 PM

I was just prepping a code sample for somebody out in the big wide
world, and doing that always makes me review my code, and I noticed I
was using Array#push a lot instead of << (I think that's Array#<<
actually?).

So, kind of a basic question, is << basically just idiomatic Ruby for
push? (This was a Rails project with a lot of custom JavaScript and I
think I just got in that particular mindset.)

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...

4 Answers

dblack

11/28/2006 8:26:00 PM

0

Ara.T.Howard

11/28/2006 8:36:00 PM

0

dblack

11/28/2006 8:39:00 PM

0

Giles Bowkett

11/29/2006 1:03:00 AM

0

> >>> I was just prepping a code sample for somebody out in the big wide
> >>> world, and doing that always makes me review my code, and I noticed I
> >>> was using Array#push a lot instead of << (I think that's Array#<<
> >>> actually?).
> >>>
> >>> So, kind of a basic question, is << basically just idiomatic Ruby for
> >>> push? (This was a Rails project with a lot of custom JavaScript and I
> >>> think I just got in that particular mindset.)
> >>
> >> I *think* the only difference is that push can take more than one
> >> argument.
> >
> > also
> >
> > << : generally returns self to allow chaining
> >
> > push : generally returns the elements pushed
>
> I'm not seeing that:
>
> irb(main):003:0> [1,2,3].push(4,5)
> => [1, 2, 3, 4, 5]
> irb(main):004:0> [1,2,3] << 4
> => [1, 2, 3, 4]

ah, that explains something. I couldn't open up my old code without
fiddling with it a bit, but changing << to push() across the board
killed something in the functionality. I didn't have time to find out
exactly what, but I think it's the only-one-arg thing that did it.

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...