Ruby nuby -- modifying Regexp casefold

L

Luca Pireddu

Hi all. I have just started playing with Ruby and I'm stuck on something. Is
there some trick to modify the casefold attribute of a Regexp object after
it's been instantiated?

The best idea I've had is the following:
irb(main):001:0> a = Regexp.new('a')
=> /a/
irb(main):002:0> aA = Regexp.new(a.to_s, Regexp::IGNORECASE)
=> /(?-mix:a)/i

But it doesn't seem to work:
irb(main):003:0> a =~ 'a'
=> 0
irb(main):004:0> a =~ 'A'
=> nil
irb(main):005:0> aA =~ 'a'
=> 0
irb(main):006:0> aA =~ 'A'
=> nil

Something I don't understand is happening here. Where did the 'i' go on line
14?
irb(main):013:0> aA = Regexp.new(a.to_s, Regexp::IGNORECASE)
=> /(?-mix:a)/i
irb(main):014:0> aA.to_s
=> "(?-mix:a)"
irb(main):015:0>

I'm using Ruby 1.8.2 (2004-08-24) [i386-linux], irb 0.9(02/07/03) on Debian.

Thanks in advance!

Luca
 
S

Simon Strandgaard

Hi all. I have just started playing with Ruby and I'm stuck on something.
Is there some trick to modify the casefold attribute of a Regexp object
after it's been instantiated?

when Regexp has been instantiated, then you cannot modify it.
There is no way to modify a regexp instance.

The best idea I've had is the following:
irb(main):001:0> a = Regexp.new('a')
=> /a/
irb(main):002:0> aA = Regexp.new(a.to_s, Regexp::IGNORECASE)
=> /(?-mix:a)/i

(?-i) turns off ignorcase mode..

But it doesn't seem to work:
irb(main):003:0> a =~ 'a'
=> 0
irb(main):004:0> a =~ 'A'
=> nil
irb(main):005:0> aA =~ 'a'
=> 0
irb(main):006:0> aA =~ 'A'
=> nil

maybe try this if you want line 006 to return an index
aA=Regexp.new('(?i)'+a.source)

Something I don't understand is happening here. Where did the 'i' go on
line 14?
irb(main):013:0> aA = Regexp.new(a.to_s, Regexp::IGNORECASE)
=> /(?-mix:a)/i

ruby 1.8.1 outputs => /(?i-mx:a)/
something must have been changed with 1.8.2
the output on line 14 is wrong.
irb(main):014:0> aA.to_s
=> "(?-mix:a)"
irb(main):015:0>

I'm using Ruby 1.8.2 (2004-08-24) [i386-linux], irb 0.9(02/07/03) on
Debian.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top