Regular expression object question

I wouldn’t go out of my way to prevent people from shooting themselves in
the foot with pointless syntax like “” + // or /ab/ + “|”. My answer would
simply be, “don’t do that”.

As you mentioned, retaining options is a problem, since the source() method
doesn’t retain that portion. I’m not sure how you would deal with this in
1.6 (I haven’t looked at this in 1.7).

/me thinks about this some more.

Regards,

Dan

···

At Wed, 25 Sep 2002 06:57:01 +0900, > Joel VanderWerf wrote:

irb(main):001:0> /ab/+“cd”
NameError: undefined method `+’ for /ab/:Regexp
from (irb):1
irb(main):002:0> class Regexp
irb(main):003:1> def +(str)
irb(main):004:2> Regexp.new(source + str)
irb(main):005:2> end
irb(main):006:1> end
nil
irb(main):007:0> /ab/+“cd”
/abcd/

Seems reasonable. But then you would also want to test for a regex
argument (and you can’t just apply #to_s), and possibly
other arguments.

Is there a reason this doesn’t already exist?

I don’t know the reasons but I can imagine

  • Other #+ hardly raise but Regexp#+ does easily: /ab/ + “|”

  • source+Regexp::escape(str) is sefe but too rigid

  • Options wounld not be preserved: /<./m + “>” != /<.>/m

  • Ruby 1.7 has Regexp#options but what is reasonable //mu + //nx

  • What should happen: “” + //