Shouldn’t this work?
[].concat(“abc”.chars)
Seems extraneous that one would have to do:
[].concat(“abc”.chars.to_a)
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:
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.
On Thu, Dec 6, 2012 at 10:43 AM, Intransition [email protected]
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
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!
On Sun, Dec 9, 2012 at 7:24 PM, Marc H. [email protected]
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
Who uses .push when .<< is so much nicer.
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 K. [email protected]
wrote:
robert
–
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
–
Matthew K., 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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs