Regexp#options (RCR #43)

D

Daniel Berger

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
 
R

Reimer Behrends

Daniel Berger ([email protected]) wrote:
[...]
# 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
 
N

nobu.nokada

Hi,

At Mon, 30 Jun 2003 09:40:48 +0900,
Daniel said:
# 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top