From: Daniel DeLorme Date: 2007-02-28T14:54:50+09:00 Subject: How can I monitor a Regexp? Because a regular expression can have different behaviors depending on its kcode (e.g. behavior of \w) I decided that all my code should specify the kcode explicitly (e.g. /\w+/n instead /\w+/). So I tried to set up some hooks to monitor the creation of each Regexp and raise an exception if the kcode is missing. Like this: class Regexp alias old_initialize initialize def initialize(*args) old_initialize(*args) raise "NO KCODE!" if kcode.nil? end end And it works fine if I use Regexp.new, but in the majority of cases the regexp is expressed as a literal and the initialize is NOT EXECUTED. > Regexp.new("foobar") RuntimeError: NO KCODE! > /foobar/ => /foobar/ So I tried an alternate approach and set the hook into the =~ operator, but same problem; the method override is completely ignored: class String; def =~(o); raise "S"; end; end class Regexp; def =~(o); raise "R"; end; end "bar" =~ /bar/ #=> 0 /foo/ =~ "foo" #=> 0 So... anyone has any idea how I can tackle that problem?