Ruby array operations
Iterate over an array, either with or without an index
ruby
a = ["a", "b", "c"]
a.each { |x| puts x } => nil
a.map { |x| x + x } => [ "aa", "bb", "cc" ]
a.each_with_index { |x, i| puts "#{i}: #{x}" } => nil
# Outputs:
# 0: "a"
# 1: "b"
# 2: "c"
What is the "right" way to iterate through an array in Ruby? - Stack Overflow
Counting occurences
For a simple boolean case:
shell
responses = [true, false, false, true, true]
yeses = responses.count(true) # => 3
See Ruby/RoR - Count occurrence of an element in an array - Stack Overflow
Enumerable
To start, add to your file:
ruby
include Enumerable
each_cons(n)
Used for iterating over a group of elements at the same time
The size of the group is the argument to the function
See Module: Enumerable (Ruby 2.3.1)
.all?
Or, to reduce a list into a boolean for all entries matching a condition:
ruby
def all_are_even(list)
list.all? { |num| num % 2 == 0 }
end
See Module: Enumerable (Ruby 1.8.7)
Duplicate an array, but remove an element given an index
ruby
a = ["a", "b", "c"]
b = a.dup.tap{|i| i.delete_at(0)} => ["b", "c"]