Enumerators: Ruby Docs Flashcards
1
Q
what value does this return??
A
arr = [“ant”, “bear”, “wolverine”]
arr.all? { |x| x != “dragon”}
——
returns true #==> no value is “dragon”
2
Q
Given a collection, how do you enumerate over each item and return the values based on if they meet the block’s test or not?
A
arr= ["dragon", "alpha", "donkey", "doofus", "ranger", "dumbledore", "domain"] arr.chunk { |n| n[0] == "d"}.each { |dLetter, ary| p [dLetter, ary]} --------- RETURNS [true, ["dragon"]] [false, ["alpha"]] [true, ["donkey", "doofus"]] [false, ["ranger"]] [true, ["dumbledore", "domain"]] => nil