:
def odds_and_evens(string, return_odds)
string.chars.each_slice(2).collect {|pair| return_odds ? pair.last : pair.first }
end
p odds_and_evens("abcdefgh", false)
#=> ["a", "c", "e", "g"]
p odds_and_evens("abcdefgh", true)
#=> ["b", "d", "f", "h"]
:
def odds_and_evens(string, return_odds)
indices = (0...string.size).select {|i| return_odds ? i.odd? : i.even? }
string.chars.values_at(*indices)
end
source
share