Lambdas Flashcards

You may prefer our related Brainscape-certified 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly