Regexp#options (RCR #43)

Hi all,

As I was going through the accepted RCR’s, came across
#43, which was accepted. This added the 'options()'
method for Regexp objects.

Testing this with 1.8 (2003-06-28) I immediately
noticed two things. First, flags to Regexp are
ignored, which I didn’t know. Second, I’m confused as
to what is supposed to be returned.

example using longhand

irb(main):001:0> r =
Regexp.new(/\w+/,Regexp::EXTENDED)
(irb):1: warning: flags ignored
=> /\w+/

example using shorthand

irb(main):002:0> r.options
=> 0irb(main):003:0> r = /\w+/xi
=> /\w+/ix
irb(main):004:0> r.options
=> 3

My guess is that it’s returning length + 1, instead of
the options themselves. If my opinion carries any
weight, I would like r.options (using the last
example) to return [“x”,“i”].

Is this a bug?

Regards,

Dan

···

Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

[…]

example using longhand

irb(main):001:0> r =
Regexp.new(/\w+/,Regexp::EXTENDED)
(irb):1: warning: flags ignored
=> /\w+/

Try

Regexp.new("\\w+", Regexp::EXTENDED)

There’s really only a point to giving flags when you compile a
string; if you pass a regular expression, you can already provide
the flags.

example using shorthand

irb(main):002:0> r.options
=> 0irb(main):003:0> r = /\w+/xi
=> /\w+/ix
irb(main):004:0> r.options
=> 3

Regexp#options returns the logical or of all flags.

My guess is that it’s returning length + 1, instead of
the options themselves. If my opinion carries any
weight, I would like r.options (using the last
example) to return [“x”,“i”].

class Regexp
def verbose_options
opts = options
result =
for i, name in [[IGNORECASE, “i”], [EXTENDED, “x”], [MULTILINE, “m”]] do
if (opts & i) != 0 then
result << name
end
end
result
end
end

			Reimer Behrends
···

Daniel Berger (djberg96@yahoo.com) wrote:

Hi,

example using longhand

irb(main):001:0> r =
Regexp.new(/\w+/,Regexp::EXTENDED)
(irb):1: warning: flags ignored
=> /\w+/

Because you passed a Regexp, which has its own options.

irb(main):001:0> r = Regexp.new(‘\w+’, Regexp::EXTENDED)
=> /\w+/x
irb(main):002:0> r.options
=> 2

example using shorthand

irb(main):002:0> r.options
=> 0irb(main):003:0> r = /\w+/xi
=> /\w+/ix
irb(main):004:0> r.options
=> 3

My guess is that it’s returning length + 1, instead of
the options themselves. If my opinion carries any
weight, I would like r.options (using the last
example) to return [“x”,“i”].

BIT OR-ed flags.

irb(main):003:0> Regexp::EXTENDED
=> 2
irb(main):004:0> Regexp::IGNORECASE
=> 1

···

At Mon, 30 Jun 2003 09:40:48 +0900, Daniel Berger wrote:


Nobu Nakada