Ruby Find Next in Array

Ruby find next in array

Array includes Enumerable, so you can use each_with_index:

elements.each_with_index {|element, index|
next_element = elements[index+1]
do_something unless next_element.nil?
...

}

How to find the next element given a certain element in array

You could modify your method, taking array size into account, like this:

array = %w[a f c e]

def find_element_after(element, array)
index = array.find_index(element) + 1
array.at(index % array.size) # divide calculated index modulo array size
end

find_element_after('e', array)
# => "a"

If you want to make your method proof to passing argument that isn't member of array, you could do:

def find_element_after(element, array)
index = array.find_index(element)
array.at((index + 1) % array.size) if index
end
find_element_after('u', array)
# => nil

or:

def find_element_after(element, array)
return nil unless array.include?(element)
index = array.find_index(element)
array.at(index % array.size)
end

as you see, there's many possible solutions. Feel free to experiment.

Ruby : How to take the next element of a array?

Array#each returns an Enumerator:

arr = [1, 2, 3]
enum = arr.each

enum.next
#=> 1

enum.next
#=> 2

enum.next
#=> 3

enum.next
#=> StopIteration: iteration reached an end

Update

Regarding your comment

I have a array with some datas, and I wanted to save them in a hash with names like... {Name : aaaaa, First Name : bbbbbb} etc etc etc

Rather than calling next over and over again (I assume you are doing something like this):

data = ["John", "Doe"]
enum = data.each
hash = {}
hash[:first_name] = enum.next
hash[:last_name] = enum.next
# ...

You can combine two arrays with Array#zip and convert it to a hash using Array#to_h:

data = ["John", "Doe"]
keys = [:first_name, :last_name, :other]

keys.zip(data).to_h
#=> {:first_name=>"John", :last_name=>"Doe", :other=>nil}

How to get the next element in an array but get first if current is last

Just out of curiosity, there is plain old good arithmetics (modulo calc) still alive:

array = %w(#646C74 #F68848 #1FA45C)
index = 1234
item = array[index % array.size]

Trying to get the previous and next item of an array using index

You're using num as an array, when its an element.

I think you meant:

array.each_with_index do |num, i|
if i == 0
array_final.push (array[i+1])
elsif i == last_index
array_final.push (num*array[i-1])
else
array_final.push(array[i+1]*array[i-1])
end
end

How to get the next and previous element in an array, Ruby

This would work:

array = [4, 1, 6, 7, 9, 3, 0]

[nil, *array, nil].each_cons(3).map { |l, m, r| (l || m) * (r || m) }
#=> [4, 24, 7, 54, 21, 0, 0]

The array is surrounded by nil values, so each element has neighbors. each_cons(3) then yiels each element along with its neighbors to map which multiplies the left (l) with the right (r) neighbor, falling back to the middle element (m) if one of the neighbors happens to be nil.

Find value in an array

If you're trying to determine whether a certain value exists inside an array, you can use Array#include?(value):

a = [1,2,3,4,5]
a.include?(3) # => true
a.include?(9) # => false

If you mean something else, check the Ruby Array API



Related Topics



Leave a reply



Submit