Lambdas Flashcards
1
Q
Symbolize the following array of numbers into symbols using your knowledge on lambdas
numbers = [1, 2, 3]
A
numbers = [1, 2, 3]
symbolize = lambda { |num| num.to_sym }
symbols = numbers.collect(&symbolize)
print symbols
2
Q
Use a lambda to check whether the following items in the my_array are symbols and at the end will print them out
my_array = [“raindrops”, :kettles, “whiskers”, :mittens, :packages]
A
my_array = [“raindrops”, :kettles, “whiskers”, :mittens, :packages]
symbol_filter = lambda { |x| x.is_a? Symbol }
symbols = my_array.select(&symbolize)
print symbols