How to Iterate Through an Array Starting from the Last Element? (Ruby)

How to iterate through an array starting from the last element? (Ruby)

array.reverse.each { |x| puts x }

how to iterate through array from the last element in Ruby

use reverse_each

presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
presidents.reverse_each { |president| p president }

What is the right way to iterate through an array in Ruby?

This will iterate through all the elements:

array = [1, 2, 3, 4, 5, 6]
array.each { |x| puts x }

# Output:

1
2
3
4
5
6

This will iterate through all the elements giving you the value and the index:

array = ["A", "B", "C"]
array.each_with_index {|val, index| puts "#{val} => #{index}" }

# Output:

A => 0
B => 1
C => 2

I'm not quite sure from your question which one you are looking for.

Iterating over each element of an array, except the first one

Using the internal enumerator is certainly more intuitive, and you can do this fairly elegantly like so:

class Array
def each_after(n)
each_with_index do |elem, i|
yield elem if i >= n
end
end
end

And now:

arr.each_after(1) do |elem|
...
end

Iterate over a array and returning the next and the element before current

Take look at Enumerable#each_cons method:

[nil, *array, nil].each_cons(3){|prev, curr, nxt|
puts "prev: #{prev} curr: #{curr} next: #{nxt}"
}

prev: curr: a next: b
prev: a curr: b next: c
prev: b curr: c next:

If you like, you can wrap it in new Array method:

class Array
def each_with_prev_next &block
[nil, *self, nil].each_cons(3, &block)
end
end
#=> nil

array.each_with_prev_next do |prev, curr, nxt|
puts "prev: #{prev} curr: #{curr} next: #{nxt}"
end

How to loop through an array using .each starting at an index in Ruby

You can also do

array.drop(n).each { |v| puts v }

I need to iterate over an array and add elements in certain conditions

As a general rule, there is never a need to use loops in Ruby. You should always prefer to use higher-level iteration constructs. Also, never mutate a data structure while you are iterating over it. (Of course, if you know me, you know I am a fan of functional programming, and thus I would say, never mutate a data structure at all.)

Here's a couple of ideas how I would solve the problem:

indices = vet.
each_cons(2).
with_index.
map {|pair, index| if pair == [2, 2] then index end }.
compact

indices.reverse_each {|index| vet.insert(index + 1, -1) }

This actually mutates vet and does so while iterating over it, which are both things I said above not to do. However, it does so in a safe manner, by extending the array from the back so that the indices yet to be processed don't shift around.

It also uses the rather "low-level" Enumerable#reverse_each iterator, which is actually not much better than a loop.

Here is a version that does not mutate vet and uses higher-level iterators:

([nil] + vet).
each_cons(2).
flat_map {|a, b| if [a, b] == [2, 2] then [-1, b] else b end }

Note that even without the bug, the code in your question is still wrong: you say you want to place a -1 between all the 2s of the array. However, your code places a -1 after every 2.

So, for example, if the input is [2], your code will produce [2, -1] instead of the correct result, which is [2].



Related Topics



Leave a reply



Submit