Last active
August 29, 2015 14:07
-
-
Save jrunning/1189951c444934e29f81 to your computer and use it in GitHub Desktop.
Fun with to_proc
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
class Regexp | |
def to_proc | |
proc {|str| match(str) && $~.to_s } | |
end | |
end | |
arr = [ "foo", "bar 123", "baz", "456", "789 qux" ] | |
p arr.find(&/bar/) # => "bar 123" | |
exp = /\d+/ | |
p arr.select(&exp) # => [ "bar 123", "456", "789 qux" ] | |
p arr.map(&exp) # => [ nil, "123", nil, "456", "789" ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment