Array#concat Enumerator

Shouldn't this work?

  [].concat("abc".chars)

Seems extraneous that one would have to do:

  [].concat("abc".chars.to_a)

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 via http://www.ruby-forum.com/.

Then do

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

Cheers

robert

···

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)

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

Who uses .push when .<< is so much nicer.

···

--
Posted via http://www.ruby-forum.com/.

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!

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

···

On Sun, Dec 9, 2012 at 7:24 PM, Marc Heiler <lists@ruby-forum.com> wrote:

Who uses .push when .<< is so much nicer.

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

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:

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

--
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