On Thu, 2006-03-23 at 23:13 +0900, Une bévue wrote:
> i'm experiencing with regexp and ruby and follo the page
> <http://www.rubycentral.com/book/ref_c_regexp.html>
> 
> at "===" i've found an example :
> 
> a = "HELLO"
> case a
> when /^a-z*$/; print "Lower case\n"
> when /^A-Z*$/; print "Upper case\n"
> else;            print "Mixed case\n"
> end
> 
> saying it produces :
> # => Upper case
> 
> i got :
> 
> Mixed case
> 
> why this discrapency, if any ???
> 

I guess that's a typo on the site, it's correct in my printed 2nd ed:

a = "HELLO"
case a
when /^[a-z]*$/; print "Lower case\n"
when /^[A-Z]*$/; print "Upper case\n"
else;            print "Mixed case\n"
end

# -> Upper Case

(Notice the [] brackets that denote a character set)

> also with :
> truc = 'toto'
> rgx=Regexp.new('^toto$')
> flag=(truc =~ rgx)? true : false
> p "flag = #{flag}"
> # => "flag = true"
> flag=(truc === rgx) /// this one i don't understand the result
> p "flag = #{flag}"
> # => "flag = false"
> flag=(truc == rgx)
> p "flag = #{flag}"
> # => "flag = false"
> flag=(truc =~ rgx)
> p "flag = #{flag}"
> # => "flag = 0"
> 
> why, when aString = 'toto' and ARegexp = Regexp.new('^toto$')
> 
> the equality "===" returns false ???
> 

Try this:

truc = 'toto'
rgx=Regexp.new('^toto$')
flag=(truc =~ rgx)? true : false
p "flag = #{flag}"
# => "flag = true"
flag=(rgx === truc) # /// switch these around
p "flag = #{flag}"
# => "flag = true"
flag=(truc == rgx)
p "flag = #{flag}"
# # => "flag = false"
flag=(truc =~ rgx)
p "flag = #{flag}"
# # => "flag = 0"

See also the 'what on earth...' thread that's been on the list recently
(yesterday/today I think).

-- 
Ross Bamford - rosco / roscopeco.REMOVE.co.uk