Skip to content

Instantly share code, notes, and snippets.

@jrunning
Last active August 29, 2015 14:07
Show Gist options
  • Save jrunning/1189951c444934e29f81 to your computer and use it in GitHub Desktop.
Save jrunning/1189951c444934e29f81 to your computer and use it in GitHub Desktop.
Fun with to_proc
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