Forum: Ruby Array#concat Enumerator

Posted by Thomas Sawyer (7rans)
on 2012-12-06 10:44
(Received via mailing list)
Shouldn't this work?

  [].concat("abc".chars)

Seems extraneous that one would have to do:

  [].concat("abc".chars.to_a)
Posted by Brian Candler (candlerb)
on 2012-12-06 18:50
Well, the documentation of concat is explicit that it requires an array 
argument:

  ary.concat(other_ary)   -> ary
------------------------------------------------------------------------------
Appends the elements of other_ary to self.

  [ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ]

I suppose it could call to_a or Array() implicitly on its argument, but 
that might have unforeseen behaviour.

Otherwise it could call other_ary.each and add the yielded elements one 
by one, which would work with anything that duck-types Enumerable. 
However I imagine this would be a lot slower if you're actually just 
concatenating an Array.

I would quite like all Enumerables to be lazy - so for example arr.map{} 
returns an Enumerator, and you can run map and select on infinite 
enumerations. If this were the case, concat would be lazy too. But 
that's not going to happen.
Posted by Robert Klemme (robert_k78)
on 2012-12-08 11:08
(Received via mailing list)
On Thu, Dec 6, 2012 at 10:43 AM, Intransition <transfire@gmail.com> 
wrote:
> Shouldn't this work?
>
>   [].concat("abc".chars)
>
> Seems extraneous that one would have to do:
>
>   [].concat("abc".chars.to_a)

Then do

irb(main):013:0> ["foo"].push(*"abc".chars)
=> ["foo", "a", "b", "c"]

Cheers

robert
Posted by Thomas Sawyer (7rans)
on 2012-12-09 19:02
(Received via mailing list)
>
>
> irb(main):013:0> ["foo"].push(*"abc".chars)
> => ["foo", "a", "b", "c"]
>
>
A bit beside the point though. I was wondering if #concat should be able 
to
handle an Enumerator.

But I didn't know push could take more than one argument, so that's
something learned today. Thanks!
Posted by Marc Heiler (shevegen)
on 2012-12-09 19:24
Who uses .push when .<< is so much nicer.
Posted by Robert Klemme (robert_k78)
on 2012-12-09 23:16
(Received via mailing list)
On Sun, Dec 9, 2012 at 7:24 PM, Marc Heiler <lists@ruby-forum.com> 
wrote:
> Who uses .push when .<< is so much nicer.

That lies in the eye of the beholder, doesn't it?  A typical use case
is when you want to append several values which are not stored in an
Enumerable.  Using << can become quite lengthy because " << " takes
more space than ", ".

Kind regards

robert
Posted by Matthew Kerwin (mattyk)
on 2012-12-10 00:44
(Received via mailing list)
I've also used #push when I want to make it explicitly clear that I'm 
using
#push and #pop as opposed to #shift and #unshift, especially when both 
sets
of operations are possible in the same code (e.g. when using a 
double-ended
queue).


On 10 December 2012 08:15, Robert Klemme <shortcutter@googlemail.com> 
wrote:

> robert
>
> --
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/
>
>


--
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.