Jamis Buck <jgb3@email.byu.edu> writes:
[ ... ]
I realize that particular question is most likely impossible to answer
for the general case. What I was wanting, though, was to be able to know
whether /\/blah/ is equal to %r{/blah}. Surely that's not too hard?
As I said, though, converting them both to strings gives the same
result, so the strings, at least, are comparable with expected results.
OK. Now I better understand your question. I agree with you that
something seems kind of counter-intuitive here:
irb(main):001:0> x = /\/blah/
=> /\/blah/
irb(main):002:0> y = %r{/blah}
=> /\/blah/
irb(main):003:0> z = %r{\/blah}
=> /\/blah/
irb(main):004:0> x == y
=> false
irb(main):005:0> x == z
=> true
irb(main):006:0> x.source
=> "\\/blah"
irb(main):007:0> y.source
=> "/blah"
irb(main):008:0> z.source
=> "\\/blah"
In line 006, why is the source of /\/blah/ returned as "\\/blah"?
It seems to me that it's incorrect to return the leading backslash
doubled. Isn't the backslash in the initial definition of x just an
escape character for enabling the second forward slash to be treated as
part of the regexp? Why is that initial escape character treated as a
bona fide part of the regexp source?
Also, consider this:
irb(main):001:0> x = /\/blah/
=> /\/blah/
irb(main):002:0> y = %r{/blah}
=> /\/blah/
irb(main):003:0> z = %r{\/blah}
=> /\/blah/
irb(main):004:0> x.to_s
=> "(?-mix:\\/blah)"
irb(main):005:0> y.to_s
=> "(?-mix:\\/blah)"
irb(main):006:0> z.to_s
=> "(?-mix:\\/blah)"
irb(main):007:0> x.to_s == y.to_s
=> true
irb(main):008:0> x.to_s == z.to_s
=> true
In other words, the string representation of the 3 regexp's (as opposed
to their _source_ representations) are identical.
So this brings up two questions:
1. Why is the backslash in a statement like this /\/blah/ _not_ just
treated as an escape character when determining the source?
2. Why does the `==' operator compare the source and not the internal
representations? It seems to me that the internal representation is
a much more meaningful piece of information for use in a comparison.
···
--
Lloyd Zusman
ljz@asfast.com
God bless you.