Enumerables Quiz
What does [1, 2, 3, 4].all? {|int| int.even?} return?
truefalse2[1, 4, 9, 16]false.What does [1, 2, 3, 4].none? {|int| int.even?} return?
truefalse2[1, 4, 9, 16]false.What does [1, 2, 3, 4].any? {|int| int.even?} return?
truefalse2[1, 4, 9, 16]true.What does [1, 2, 3, 4].map {|int| int ** 2} return?
truefalse2[1, 4, 9, 16]map returns a new array with each of the elements in the receiver squared.What does [1, 2, 3, 4].count {|int| int.even?} return?
truefalse2[1, 4, 9, 16]2 and 4) return truthy values when passed to the block. The return value is therefore 2.innocent_idiom = ["We", "were", "as", "Danes", "in", "Denmark", "all", "day", "long"]
how_long_am_i = innocent_idiom.select {|word| word.length < 3} + innocent_idiom.reject {|word| word.length < 3}
What is the number of elements in how_long_am_i?
9630select and reject are complementary. select returns all the elements in its receiver for which the given block returns a truthy value, and reject returns all those that result in a falsey value. Concatenating the results of invoking both with the same block on the same receiver therefore produces exactly all the elements in that receiver: no more no less. def num_vowels(word)
vowels = ["a", "e", "i", "o", "u"]
word.chars.count {|ch| vowels.include?(ch.downcase)}
end
def order_by_num_vowels(str)
words = str.split
words.sort_by {|word| num_vowels(word)}
end
What is the return_value of order_by_num_vowels("Miracle bird or golden handiwork")?
10["handiwork"]["bird", "or", "golden", "Miracle", "handiwork"]["handiwork", "Miracle", "golden", "or", "bird"]order_by_num_vowels sorts the words of a given string by the number of vowels in each and returns the resulting array. The correct answer is therefore ["bird", "or", "golden", "Miracle", "handiwork"].spheres = ["Moon", "Mercury", "Venus", "Sun", "Mars", "Jupiter",
"Saturn", "Fixed Stars", "Primum Mobile"]
numbers_and_spheres = []
spheres.each_with_index do |circle, idx|
numbers_and_spheres << "#{idx + 1}: #{circle}"
end
What is the value of numbers_and_spheres at the end of the above code snippet?
["Moon", "Mercury", "Venus", "Sun", "Mars", "Jupiter",
"Saturn", "Fixed Stars", "Primum Mobile"]["1: Moon", "2: Mercury", "3: Venus", "4: Sun", "5: Mars", "6: Jupiter", "7: Saturn", "8: Fixed Stars", "9: Primum Mobile"]["0: Moon", "1: Mercury", "2: Venus", "3: Sun", "4: Mars", "5: Jupiter", "6: Saturn", "7: Fixed Stars", "8: Primum Mobile"][0, 1, 2, 3, 4, 5, 6, 7, 8]each_with_index enumerable to populate numbers_and_spheres with strings containing indices incremented by one and the names of the Spheres of Heaven in Dante's Paradiso.