# Newbie, questions about === expression

**URL:** <https://rubytalk.org/t/newbie-questions-about-expression/66566>\
**Category:** ruby-talk\
**Created:** [27 October 2012 14:26 UTC](https://rubytalk.org/t/newbie-questions-about-expression/66566 "2012-10-27T14:26:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11180](https://avatars.discourse-cdn.com/v4/letter/1/aca169/32.png) [@11180](https://rubytalk.org/u/11180)\
**Post date:** [27 October 2012 14:26 UTC](https://rubytalk.org/t/newbie-questions-about-expression/66566/1 "2012-10-27T14:26:57Z")

</div>

Hi,

I'm learning ruby this days.  
And I followed this docs: [http://www.ruby-doc.org/docs/ProgrammingRuby/](http://www.ruby-doc.org/docs/ProgrammingRuby/)

Now, I have some questions about === expression

irb(main):138:0\> case 1  
irb(main):139:1\> when 1..10 then 'in'  
irb(main):140:1\> else 'not in'  
irb(main):141:1\> end

obviously, the result is 'in'

In the docs, there are an explanation:  
\*case operates by comparing the target (the expression after the keyword  
case) with each of the comparison expressions after the when keywords. This  
test is done using comparison === target. As long as a class defines  
meaningful semantics for === (and all the built-in classes do), objects of  
that class can be used in case expressions. \*

But, when I type this, why throw an error ?

irb(main):149:0\> 1..10 === 1  
ArgumentError: bad value for range  
&nbsp;&nbsp;&nbsp;&nbsp;from (irb):149  
&nbsp;&nbsp;&nbsp;&nbsp;from /opt/ruby-1.9.3/bin/irb:12:in `\<main\>'

Thanks.

---

<div class="post-metadata">

**Author:** ![11180](https://avatars.discourse-cdn.com/v4/letter/1/aca169/32.png) [@11180](https://rubytalk.org/u/11180)\
**Post date:** [27 October 2012 14:30 UTC](https://rubytalk.org/t/newbie-questions-about-expression/66566/2 "2012-10-27T14:30:22Z")

</div>

Oh, Maybe I found the reason:

irb(main):195:0\> (1..10) === 1  
=\> true

> **···**
>
> 2012/10/27 月忧茗 \<yueyoum@gmail.com\>
> 
> > Hi,
> > 
> > I'm learning ruby this days.  
> > And I followed this docs: [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/)
> > 
> > Now, I have some questions about === expression
> > 
> > irb(main):138:0\> case 1  
> > irb(main):139:1\> when 1..10 then 'in'  
> > irb(main):140:1\> else 'not in'  
> > irb(main):141:1\> end
> > 
> > obviously, the result is 'in'
> > 
> > In the docs, there are an explanation:  
> > \*case operates by comparing the target (the expression after the keyword  
> > case) with each of the comparison expressions after the when keywords.  
> > This test is done using comparison === target. As long as a class defines  
> > meaningful semantics for === (and all the built-in classes do), objects  
> > of that class can be used in case expressions. \*
> > 
> > But, when I type this, why throw an error ?
> > 
> > irb(main):149:0\> 1..10 === 1  
> > ArgumentError: bad value for range  
> > &nbsp;&nbsp;&nbsp;&nbsp;from (irb):149  
> > &nbsp;&nbsp;&nbsp;&nbsp;from /opt/ruby-1.9.3/bin/irb:12:in `\<main\>'
> > 
> > Thanks.

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [27 October 2012 14:36 UTC](https://rubytalk.org/t/newbie-questions-about-expression/66566/3 "2012-10-27T14:36:10Z")

</div>

You need to put parentheses around the range:

(1..10) === 1

otherwise ruby understands the expression as if it were  
1..(10 === 1)

that is, as

10..false,

which, of course, is not a valid range.

I hope this helps

Stefano

> **···**
>
> On Saturday 27 October 2012 月忧茗 wrote
> 
> > Hi,
> > 
> > I'm learning ruby this days.  
> > And I followed this docs: [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/)
> > 
> > Now, I have some questions about === expression
> > 
> > irb(main):138:0\> case 1  
> > irb(main):139:1\> when 1..10 then 'in'  
> > irb(main):140:1\> else 'not in'  
> > irb(main):141:1\> end
> > 
> > obviously, the result is 'in'
> > 
> > In the docs, there are an explanation:  
> > \*case operates by comparing the target (the expression after the keyword  
> > case) with each of the comparison expressions after the when keywords. This  
> > test is done using comparison === target. As long as a class defines  
> > meaningful semantics for === (and all the built-in classes do), objects of  
> > that class can be used in case expressions. \*
> > 
> > But, when I type this, why throw an error ?
> > 
> > irb(main):149:0\> 1..10 === 1  
> > ArgumentError: bad value for range  
> > &nbsp;&nbsp;&nbsp;&nbsp;from (irb):149  
> > &nbsp;&nbsp;&nbsp;&nbsp;from /opt/ruby-1.9.3/bin/irb:12:in `\<main\>'
> > 
> > Thanks.
