<< == push?

G

Giles Bowkett

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.)
 
D

dblack

Hi --

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.


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
A

ara.t.howard

Hi --



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

-a
 
D

dblack

Hi --

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]


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
G

Giles Bowkett

I was just prepping a code sample for somebody out in the big wide
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top