# Case statement ranges

**URL:** <https://www.ruby-forum.com/t/case-statement-ranges/107364>\
**Category:** Rails\
**Created:** [June 20, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/case-statement-ranges/107364 "2007-06-20T22:31:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 20, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/case-statement-ranges/107364/1 "2007-06-20T22:31:41Z")

</div>

I have the following in my controller:

def color\_coding(value = nil)  
case value  
when 101…1000000000  
“busy\_11”

```
  when 91..100
    "busy_10"

  when 81..90
    "busy_9"

  when 71..80
    "busy_8"

  when 61..70
    "busy_7"

  when 51..60
    "busy_6"

  when 41..50
    "busy_5"

  when 31..40
    "busy_4"

  when 21..30
    "busy_3"

  when 11..20
    "busy_2"

  when 1..10
    "busy_1"

  else
    "no_class"
end

```

end

Two questions:

1. 

```
  when 101..1000000000
    "busy_11"

```

is obviously an ugly hack. What I want is: output “busy\_11” if value  
is 101 or higher. Not sure how I should write this in ruby.

1. 

```
  when 91..100
 "busy_10"

```

What I want is: output “busy\_10” when value is \>= 91 and \<= 100. Not  
sure if it should be 91…100 or 91…100 or some other syntax.

Tried to google this but couldn’t find a tutorial that covers the  
above fully. Thanks in advance!

Best,

Gabor

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 20, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/case-statement-ranges/107364/2 "2007-06-20T22:35:42Z")

</div>

def color\_coding(value = nil)  
if value && value \> 0  
if value \> 100  
“busy\_11”  
else  
“busy\_” + ((value/10)+1).to\_s  
end  
end  
end

Cheers,  
Tyler

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

> ```
> "busy_6"
> when 11..20
> 
> ```
> 
> 1. 
> 
> ```
> when 91..100
> 
> ```
> 
> Gabor
> 
> >

–

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 20, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/case-statement-ranges/107364/3 "2007-06-20T22:36:42Z")

</div>

How about this (untested) instead…

def color\_coding(value = nil)  
if value \>= 101  
“busy\_11”  
elsif value \<= 0 || value.nil?  
“no\_class”  
else  
“busy\_” + (value/10.0).ceil.to\_s  
end  
end

Would be a lot shorter…

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 20, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/case-statement-ranges/107364/4 "2007-06-20T22:42:43Z")

</div>

Thanks guys! Great stuff!

But one question still remains:

what does 91…100 (two dots between the two numbers) mean, and what  
does 91…100 (three dots between the two numbers) mean in Ruby?

Best,

Gabor

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 20, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/case-statement-ranges/107364/5 "2007-06-20T22:44:43Z")

</div>

On Jun 20, 2007, at 4:34 PM, Tyler MacDonald wrote:

> Cheers,
> 
> > ```
> > when 91..100
> > 
> > "busy_3"
> > 
> > ```
> > 
> > end
> > 
> > Two questions:
> > 
> > 1. 
> > 
> > ```
> > when 101..1000000000
> > "busy_11"
> > 
> > ```
> > 
> > is obviously an ugly hack. What I want is: output “busy\_11” if value  
> > is 101 or higher. Not sure how I should write this in ruby.

when nil, 0  
“no\_class”  
else  
“busy\_11”

I’m guessing what you might want for value==0

> > Gabor

91…100 === x will test (91 \<= x && x \<= 100)

91…100 === x (three dot range excludes end) will test (91 \<= x && x  
\< 100)

What should color\_coding(100.5) give?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 20, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/case-statement-ranges/107364/6 "2007-06-20T22:48:44Z")

</div>

Hi –

On Wed, 20 Jun 2007, gabordemeter wrote:

> Thanks guys! Great stuff!
> 
> But one question still remains:
> 
> what does 91…100 (two dots between the two numbers) mean, and what  
> does 91…100 (three dots between the two numbers) mean in Ruby?

The … is inclusive; the … is exclusive.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 20, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/case-statement-ranges/107364/7 "2007-06-20T23:08:46Z")

</div>

> > But one question still remains:
> > 
> > what does 91…100 (two dots between the two numbers) mean, and what  
> > does 91…100 (three dots between the two numbers) mean in Ruby?
> 
> The … is inclusive; the … is exclusive.

Best analogy I’ve heard is that that extra dot pushes the last number  
off  
the cliff where cliff is your range.

That’s the only way I can remember it 🙂
