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 | add new comment | 418 reads

submit


maybe submit it to facets?

grep


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