Kurt Stephens

Nerd Up!

Ruby : Regexp#to_proc

Kurt on Sat, 2007-12-15 03:02.

Helpful in IRB:


class Regexp
  def to_proc
    @proc ||= lambda { | x | self.match(x) }
  end
end

As in:


irb(main):001:0> pp Object.methods.sort.select(&/meth/)
["instance_method",
 "instance_methods",
 "method",
 "method_defined?",
 "methods",
 "private_class_method",
 "private_instance_methods",
 "private_method_defined?",
 "private_methods",
 "protected_instance_methods",
 "protected_method_defined?",
 "protected_methods",
 "public_class_method",
 "public_instance_methods",
 "public_method_defined?",
 "public_methods",
 "singleton_methods"]
=> nil
irb(main):002:0> pp Object.methods.sort.find(&/def/)
"const_defined?"
=> nil

W00t!


links: Kurt's blog | 1669 reads

submit


maybe submit it to facets?

grep


there’s also instance.methods.grep /abc/

Primary links

Syndicate

Syndicate content

Browse archives

« February 2012  
Mo Tu We Th Fr Sa Su
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29