1

I feel like i've seen this question somewhere before but i cannot find it and i'm pretty sure ruby has a one liner for looping over something and adding the items to an array:

items = []
some_stuff.each do |stuff|
  items << stuff
end
items
5
  • Enumerable#reduce or Enumerable#each_with_object would do the job Commented Oct 20, 2022 at 8:40
  • reduce is not necessary, he is not asking the summation. He simply wants to push one array into another. Commented Oct 20, 2022 at 8:43
  • 1
    And who said reduce is only for summation? Reduce is Ruby's answer to fold HOF and it can do this job perfectly fine. Which one to pick is just a matter of personal preferences or conventions Commented Oct 20, 2022 at 8:54
  • 2
    From your example, it looks like items = some_stuff.dup is all you need. Or maybe items.concat(some_stuff) to append to an existing array. Commented Oct 20, 2022 at 9:30
  • 3
    Do you mean to use Enumerable#map, e.g. items = some_stuff.map { |stuff| stuff.whatever }? Your initial example is probably too simplified for what you actually intend to do. Commented Oct 20, 2022 at 10:24

4 Answers 4

1

this way?

items = some_stuff.inject([]) { |acc, item| acc + [item] }

obviously this exact snippet doesn't make sense because it's another array with the same value, you could just dup some_stuff, but I think can help you anyway

Sign up to request clarification or add additional context in comments.

4 Comments

He is not asking for summation of the items, he is asking for adding into the array which he simply can assign it.
I'm not adding anything. I do not mutate the object, that's all
Ah!!! sorry, I misunderstood you. Thanks for clarifying.
Note that this creates two new arrays on each iteration: one for [item] and one as the result of acc + [item].
1

Is this what you are looking for?

some_stuff=[1,2,3,4,6,7]

Code

items=some_stuff.each_with_object([]){|value,items|items.push(value)}
p items

Output

[1, 2, 3, 4, 6, 7]

Comments

1

If some_stuff responds to each, then it very likely also conforms to the Enumerable protocol. (It would be silly not to, since each is all you need to support the Enumerable protocol via include Enumerable.)

The Enumerable protocol includes the method Enumerable#to_a, which returns the elements of the Enumerable as an Array:

to_aarray

Returns an array containing the items in self:

(0..4).to_a # => [0, 1, 2, 3, 4]

So, assuming that some_stuff really does conform to the Enumerable protocol, all you need to do is

items = some_stuff.to_a

However, unless you absolutely need the specific features an Array provides, there is no need to even do this. If some_stuff really does conform to the Enumerable protocol, then most of the methods that you would use on the Array already exist on some_stuff as well, and they may even be more efficient because some_stuff knows more about what its internal structure is and what the structure of the elements is than Array does.

There is often no need to convert an Enumerable to an Array.

Comments

0

You could use splat operator if working with an array

items = [*some_stuff]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.