understanding aRegexp === aString

U

Une bévue

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 ???

is that due to the fact my script file is UTF-8 encoded ???

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 ???
 
R

Ross Bamford

i'm experiencing with regexp and ruby and follo the page
<http://www.rubycentral.com/book/ref_c_regexp.html>
=20
at "=3D=3D=3D" i've found an example :
=20
a =3D "HELLO"
case a
when /^a-z*$/; print "Lower case\n"
when /^A-Z*$/; print "Upper case\n"
else; print "Mixed case\n"
end
=20
saying it produces :
# =3D> Upper case
=20
i got :
=20
Mixed case
=20
why this discrapency, if any ???
=20

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

a =3D "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 =3D 'toto'
rgx=3DRegexp.new('^toto$')
flag=3D(truc =3D~ rgx)? true : false
p "flag =3D #{flag}"
# =3D> "flag =3D true"
flag=3D(truc =3D=3D=3D rgx) /// this one i don't understand the result
p "flag =3D #{flag}"
# =3D> "flag =3D false"
flag=3D(truc =3D=3D rgx)
p "flag =3D #{flag}"
# =3D> "flag =3D false"
flag=3D(truc =3D~ rgx)
p "flag =3D #{flag}"
# =3D> "flag =3D 0"
=20
why, when aString =3D 'toto' and ARegexp =3D Regexp.new('^toto$')
=20
the equality "=3D=3D=3D" returns false ???
=20

Try this:

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

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

--=20
Ross Bamford - (e-mail address removed)
 
R

Robert Klemme

Adding to Ross' excellent explanation: it helps to *not* view "===" as
an equality operator. Rather it's a general matching operator, whatever
matching means in a certain context. For RX it will do an RX match, for
class objects it will do the kind_of? check, for a range it will check
include? etc. Btw, has anyone an idea why this does not apply to
Enumerable?
[1,2,3].include? 2 => true
[1,2,3] === 2
=> false

Kind regards

robert
 
U

Une bévue

Ross Bamford said:
(Notice the [] brackets that denote a character set)

fine, thanks, difficult to catch out typos particularly in regexps...
Try this:
[...]

flag=(rgx === truc) # /// switch these around
p "flag = #{flag}"
# => "flag = true"

i did both :
flag=(truc === rgx)
p "flag = #{flag} for flag=(truc === rgx)"
# => "flag = false for flag=(truc === rgx)""
flag=(rgx === truc)
p "flag = #{flag} for flag=(rgx === truc)"
# => "flag = true for flag=(rgx === truc)"

that's a really strange behaviour, to me, of ruby, a "symetrical"
operator symbol being not commutative ????

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

ok, thanks very much !
 
J

Joel VanderWerf

Robert said:
=20
Adding to Ross' excellent explanation: it helps to *not* view "=3D=3D=3D= " as
an equality operator. Rather it's a general matching operator, whateve= r
matching means in a certain context. For RX it will do an RX match, fo= r
class objects it will do the kind_of? check, for a range it will check
include? etc. Btw, has anyone an idea why this does not apply to
Enumerable?
=20
[1,2,3].include? 2 =3D> true
[1,2,3] =3D=3D=3D 2
=3D> false

It would be nice, but then how would this work:

irb(main):001:0> case [1,2,3]
irb(main):002:1> when [1,2,3]
irb(main):003:1> puts "same array!"
irb(main):004:1> end
same array!
=3D> nil

--=20
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
 
J

Joel VanderWerf

Robert said:
=20
Adding to Ross' excellent explanation: it helps to *not* view "=3D=3D=3D= " as
an equality operator. Rather it's a general matching operator, whateve= r
matching means in a certain context. For RX it will do an RX match, fo= r
class objects it will do the kind_of? check, for a range it will check
include? etc. Btw, has anyone an idea why this does not apply to
Enumerable?
=20
[1,2,3].include? 2 =3D> true
[1,2,3] =3D=3D=3D 2
=3D> false

It would be nice, but then how would this work:

irb(main):001:0> case [1,2,3]
irb(main):002:1> when [1,2,3]
irb(main):003:1> puts "same array!"
irb(main):004:1> end
same array!
=3D> nil

--=20
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
 
S

Stephen Bannasch

I'm trying to use builder to construct jnlp files. I need to create
elements with "-" in the name and can't. For example I need to output
the following xml fragment:

<security>
<all-permissions />
</security>

Here's what I tried with builder:

x = Builder::XmlMarkup.new:)target => $stdout, :indent => 1)

x.security {
x.all-permissions
}

but the "-" is parsed in the name and this produces a NameError

NameError: undefined local variable or method `permissions' for main:Object

Is there a way to do this?
 
J

Josh Knowles

------=_Part_7051_2638651.1143156154836
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

...

x.security {
x.all-permissions
}

but the "-" is parsed in the name and this produces a NameError

NameError: undefined local variable or method `permissions' for
main:Object

Is there a way to do this?



Use the tag! method...

x.security {
x.tag! "all-permissions" {

}
}

Hope this helps.

Josh

------=_Part_7051_2638651.1143156154836--
 
S

Stephen Bannasch

Thanks Josh. That did indeed help! It is much simpler than the way I was trying to solve the problem.
 
R

Robert Klemme

YANAGAWA said:
In Message-Id: <[email protected]>
Robert Klemme said:
[1,2,3].include? 2 => true
[1,2,3] === 2
=> false

Once that can be but "fixed" later since.... it can be a seed of
confusion and we can use "*" for array on when clause.

n = 2
array = [2, 4, 6]

case n
when *array
puts "foo"
else
puts "bar"
end

# prints "foo"

Good point! Thanks to you and Joel!

Kind regards

robert
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,668
Latest member
SamiraShac

Latest Threads

Top