# Understanding aRegexp === aString

**URL:** <https://www.ruby-forum.com/t/understanding-aregexp-astring/85963>\
**Category:** Ruby\
**Created:** [March 23, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/understanding-aregexp-astring/85963 "2006-03-23T15:13:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 23, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/understanding-aregexp-astring/85963/1 "2006-03-23T15:13:56Z")

</div>

i’m experiencing with regexp and ruby and follo the page  
[http://www.rubycentral.com/book/ref\_c\_regexp.html](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 ???

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 23, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/understanding-aregexp-astring/85963/2 "2006-03-23T15:48:57Z")

</div>

Ross B. wrote:

> On Thu, 2006-03-23 at 23:13 +0900, Une bévue wrote:

> > the equality “===” returns false ???

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 23, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/understanding-aregexp-astring/85963/3 "2006-03-23T15:37:29Z")

</div>

On Thu, 2006-03-23 at 23:13 +0900, Une bÃ©vue wrote:

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

> 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).

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 23, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/understanding-aregexp-astring/85963/4 "2006-03-23T18:28:30Z")

</div>

Robert K. wrote:

> Enumerable?
> 
> > > [1,2,3].include? 2  
> > > =\> true
> > 
> > > [1,2,3] === 2  
> > > =\> 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!  
=\> nil

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 23, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/understanding-aregexp-astring/85963/5 "2006-03-23T15:49:31Z")

</div>

Ross B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (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 !

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 23, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/understanding-aregexp-astring/85963/6 "2006-03-23T23:05:54Z")

</div>

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:

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?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 23, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/understanding-aregexp-astring/85963/7 "2006-03-23T18:42:29Z")

</div>

Robert K. wrote:

> Enumerable?
> 
> > > [1,2,3].include? 2  
> > > =\> true
> > 
> > > [1,2,3] === 2  
> > > =\> 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!  
=\> nil

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 24, 2006, 10:38am UTC](https://www.ruby-forum.com/t/understanding-aregexp-astring/85963/8 "2006-03-24T10:38:59Z")

</div>

YANAGAWA Kazuhisa wrote:

> # prints “foo”

Good point! Thanks to you and Joel!

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 24, 2006, 12:18am UTC](https://www.ruby-forum.com/t/understanding-aregexp-astring/85963/9 "2006-03-24T00:18:19Z")

</div>

In Message-Id: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > > [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”
