Difficulty:
Easy
Easy
Tags:
enumerables, procs
enumerables, procs
Instructions:
The Pattern object not only functions as a Regexp. For example, check out what happens if you do Pattern === element. This problem demonstrates that using a Pattern with #grep will work much like using select and map in combination.
The Pattern object not only functions as a Regexp. For example, check out what happens if you do Pattern === element. This problem demonstrates that using a Pattern with #grep will work much like using select and map in combination.
Problem and Solution:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solution to 'Select Map' on rubeque.com | |
# by jakthegr8 | |
# http://www.rubeque.com/problems/select-map | |
assert_equal ["1", 2, "3", 4, "5"].grep(/[1-5]/){ |e| e.succ }, ["2", "4", "6"] | |
assert_equal [0, 1, 3, 5, 6].grep(1..5, &:succ), [2, 4, 6] | |
assert_equal ["1", "2", "3", "4", "5"].grep(proc{|ele| ele.to_i%2 ==1}){ |e| e.succ }, ["2", "4", "6"] | |
assert_equal [1, 2, 3, 4, 5].grep(proc{|ele| ele.to_i%2 ==1}, &:succ), [2, 4, 6] |