# Case

**URL:** <https://rubytalk.org/t/case/73994>\
**Category:** ruby-talk\
**Created:** [10 January 2017 22:16 UTC](https://rubytalk.org/t/case/73994 "2017-01-10T22:16:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)\
**Post date:** [10 January 2017 22:16 UTC](https://rubytalk.org/t/case/73994/1 "2017-01-10T22:16:32Z")

</div>

Hi

a=3  
p case a  
&nbsp;&nbsp;when 0 then 0  
&nbsp;&nbsp;when 1 then 1  
&nbsp;&nbsp;when 2 then 2  
&nbsp;&nbsp;when 3, a\>5 then 3  
&nbsp;&nbsp;else 99  
end

Seems you can't combine values and statements (a\>5).  
How can I write that, if I have additional statements (in some cases), but would like to use "values only" for most of the cases?

Thanks  
Opti

---

<div class="post-metadata">

**Author:** ![Eric\_Christopherson](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/eric_christopherson/32/1849_2.png) [@Eric\_Christopherson](https://rubytalk.org/u/Eric_Christopherson)\
**Post date:** [10 January 2017 23:22 UTC](https://rubytalk.org/t/case/73994/2 "2017-01-10T23:22:26Z")

</div>

There's no such thing as a statement in Ruby.

> **···**
>
> On Tue, Jan 10, 2017 at 4:16 PM, Die Optimisten \<inform@die-optimisten.net\> wrote:
> 
> > Seems you can't combine values and statements (a\>5).
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Eric Christopherson

---

<div class="post-metadata">

**Author:** ![Mike\_Stok1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/mike_stok1/32/2065_2.png) [@Mike\_Stok1](https://rubytalk.org/u/Mike_Stok1)\
**Post date:** [10 January 2017 23:29 UTC](https://rubytalk.org/t/case/73994/3 "2017-01-10T23:29:52Z")

</div>

You can use a lambda if you like:

irb(main):032:0\> (0..10).each do |a|  
irb(main):033:1\* p case a  
irb(main):034:2\> when 0 then 0  
irb(main):035:2\> when 1 then 1  
irb(main):036:2\> when 2 then 2  
irb(main):037:2\> when -\> (x) { x == 3 || x \> 5 } then 3  
irb(main):038:2\> else 99  
irb(main):039:2\> end  
irb(main):040:1\> end  
0  
1  
2  
3  
99  
99  
3  
3  
3  
3  
3  
=\> 0..10

Hope this helps,

Mike

> **···**
>
> > On Jan 10, 2017, at 5:16 PM, Die Optimisten \<inform@die-optimisten.net\> wrote:
> > 
> > Hi
> > 
> > a=3  
> > p case a  
> > when 0 then 0  
> > when 1 then 1  
> > when 2 then 2  
> > when 3, a\>5 then 3  
> > else 99  
> > end
> > 
> > Seems you can't combine values and statements (a\>5).  
> > How can I write that, if I have additional statements (in some cases), but would like to use "values only" for most of the cases?
> > 
> > Thanks  
> > Opti
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> 
> --
> 
> Mike Stok \<mike@stok.ca\>  
> [http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)
> 
> The "`Stok' disclaimers" apply.

---

<div class="post-metadata">

**Author:** ![James\_Pacheco](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/james_pacheco/32/2457_2.png) [@James\_Pacheco](https://rubytalk.org/u/James_Pacheco)\
**Post date:** [10 January 2017 23:10 UTC](https://rubytalk.org/t/case/73994/4 "2017-01-10T23:10:46Z")

</div>

> a=3  
> p case a  
> when 0 then 0  
> when 1 then 1  
> when 2 then 2  
> when 3, a\>5 then 3  
> else 99  
> end

a=3  
p case a  
&nbsp;&nbsp;when 0 then 0  
&nbsp;&nbsp;when 1 then 1  
&nbsp;&nbsp;when 2 then 2  
&nbsp;&nbsp;when 4..5 then 99 # using a range here to cover your original 'else'  
clause  
&nbsp;&nbsp;else 3 # this is now 'when 3, a \> 5'  
end

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 January 2017 11:27 UTC](https://rubytalk.org/t/case/73994/5 "2017-01-11T11:27:02Z")

</div>

And the generic answer for arbitrary conditions is: use a lambda,  
because my suggestion to make lambda support #=== as an alias for  
#call and # was implemented a long time ago:

irb(main):002:0\> x = lambda {|a| a \> 5}  
=\> #\<Proc:0x00000001287250@(irb):2 (lambda)\>  
irb(main):003:0\> 10.times {|i| printf "%2d -\> %p\n", i, x === i}  
0 -\> false  
1 -\> false  
2 -\> false  
3 -\> false  
4 -\> false  
5 -\> false  
6 -\> true  
7 -\> true  
8 -\> true  
9 -\> true  
=\> 10

Equipped with the knowledge that case uses === for matching you can  
easily use that here:

LARGER\_THAN\_5 = lambda {|n| n \> 5}

a=3  
p case a  
when 0 then 0  
when 1 then 1  
when 2 then 2  
when 3, LARGER\_THAN\_5 then 3  
else 99  
end

Note, you can rewrite that to

a = 3  
p case a  
when 0,1,2,3 then a  
when LARGER\_THAN\_5 then 3  
else 99  
end

or use a Hash or ...

irb(main):047:0\> FIXED = {0=\>0,1=\>1,2=\>2,3=\>3}  
irb(main):048:0\> -3.upto(10) {|i| printf "%2d -\> %p\n", i,  
FIXED.fetch(i) { i\>5 ? 3 : 99 }}  
-3 -\> 99  
-2 -\> 99  
-1 -\> 99  
0 -\> 0  
1 -\> 1  
2 -\> 2  
3 -\> 3  
4 -\> 99  
5 -\> 99  
6 -\> 3  
7 -\> 3  
8 -\> 3  
9 -\> 3  
10 -\> 3  
=\> -3

etc.

Cheers

robert

> **···**
>
> On Wed, Jan 11, 2017 at 12:10 AM, James Pacheco \<james.pacheco@gmail.com\> wrote:
> 
> > > a=3  
> > > p case a  
> > > when 0 then 0  
> > > when 1 then 1  
> > > when 2 then 2  
> > > when 3, a\>5 then 3  
> > > else 99  
> > > end
> > 
> > a=3  
> > p case a  
> > &nbsp;&nbsp;when 0 then 0  
> > &nbsp;&nbsp;when 1 then 1  
> > &nbsp;&nbsp;when 2 then 2  
> > &nbsp;&nbsp;when 4..5 then 99 # using a range here to cover your original 'else'  
> > clause  
> > &nbsp;&nbsp;else 3 # this is now 'when 3, a \> 5'  
> > end
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [11 January 2017 13:07 UTC](https://rubytalk.org/t/case/73994/6 "2017-01-11T13:07:42Z")

</div>

> p case a  
> when 0 then 0  
> when 1 then 1  
> when 2 then 2  
> when 3, a\>5 then 3  
> else 99  
> end

try the else part,

p case a  
when 0 then 0  
when 1 then 1  
when 2 then 2  
when 3 then 3  
else  
&nbsp;&nbsp;if a \> 5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;99  
&nbsp;&nbsp;end  
end

you can replace the if with another (nested) case if you're fond of case : )

> Seems you can't combine values and statements (a\>5)

try the other case and other combinations case w case, case w if, etc,

p case  
when [0,1,2,3].include?(a)  
&nbsp;&nbsp;&nbsp;a  
when a \> 5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3  
else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;99  
end

kind regards  
--botp

> **···**
>
> On Wed, Jan 11, 2017 at 6:16 AM, Die Optimisten \<inform@die-optimisten.net\> wrote:

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [11 January 2017 12:30 UTC](https://rubytalk.org/t/case/73994/7 "2017-01-11T12:30:07Z")

</div>

> And the generic answer for arbitrary conditions is: use a lambda,  
> because my suggestion to make lambda support #=== as an alias for  
> #call and # was implemented a long time ago:

As the great Alan Perlis didn't quite say:

> [Ruby] programmers know the value of everything and the cost of nothing.

Generic answers are great for generic questions, but using a proc in this case (no pun intended) comes with a very large cost. I really liked James' suggestion, to flip the logic around, but I knew that the range was

What I'm truly surprised by is James' solution being as slow as it was. I figured it had to beat out proc activation. I'm also a bit surprised at the cost of my valueless case (ryan2).

---- benchmark and results follow

require 'benchmark/ips'

LARGER\_THAN\_5 = lambda { |m| m \> 5}

def robert n  
&nbsp;&nbsp;case n  
&nbsp;&nbsp;when 0 then 0  
&nbsp;&nbsp;when 1 then 1  
&nbsp;&nbsp;when 2 then 2  
&nbsp;&nbsp;when 3, LARGER\_THAN\_5 then 3  
&nbsp;&nbsp;else 99  
&nbsp;&nbsp;end  
end

def robert2 n  
&nbsp;&nbsp;case n  
&nbsp;&nbsp;when 0,1,2,3 then n  
&nbsp;&nbsp;when LARGER\_THAN\_5 then 3  
&nbsp;&nbsp;else 99  
&nbsp;&nbsp;end  
end

def mike n  
&nbsp;&nbsp;case n  
&nbsp;&nbsp;when 0 then 0  
&nbsp;&nbsp;when 1 then 1  
&nbsp;&nbsp;when 2 then 2  
&nbsp;&nbsp;when -\> (x) { x == 3 || x \> 5 } then 3  
&nbsp;&nbsp;else 99  
&nbsp;&nbsp;end  
end

def james n  
&nbsp;&nbsp;case n  
&nbsp;&nbsp;when 0 then 0  
&nbsp;&nbsp;when 1 then 1  
&nbsp;&nbsp;when 2 then 2  
&nbsp;&nbsp;when 4..5 then 99 # using a range here to cover your original 'else' clause  
&nbsp;&nbsp;else 3 # this is now 'when 3, a \> 5'  
&nbsp;&nbsp;end  
end

def ryan n  
&nbsp;&nbsp;case n  
&nbsp;&nbsp;when 0 then 0  
&nbsp;&nbsp;when 1 then 1  
&nbsp;&nbsp;when 2 then 2  
&nbsp;&nbsp;when 4, 5 then 99 # because range will bite you  
&nbsp;&nbsp;else 3  
&nbsp;&nbsp;end  
end

def ryan2 n  
&nbsp;&nbsp;case  
&nbsp;&nbsp;when n==0 then 0  
&nbsp;&nbsp;when n==1 then 1  
&nbsp;&nbsp;when n==2 then 2  
&nbsp;&nbsp;when n==3 || n\>5 then 3 # just to see the impact of a valueless case  
&nbsp;&nbsp;else 99  
&nbsp;&nbsp;end  
end

10.times do |n|  
&nbsp;&nbsp;v0, v1, v2, v3, v4, v5 =  
&nbsp;&nbsp;&nbsp;&nbsp;robert(n), robert2(n), mike(n), james(n), ryan(n), ryan2(n)

&nbsp;&nbsp;abort "bad" if [v0, v1, v2, v3, v4, v5].uniq.size != 1  
end

Benchmark.ips do |x|  
&nbsp;&nbsp;x.report("robert") do |t| t.times do 10.times do |n| robert n end end end  
&nbsp;&nbsp;x.report("robert2") do |t| t.times do 10.times do |n| robert2 n end end end  
&nbsp;&nbsp;x.report("james") do |t| t.times do 10.times do |n| james n end end end  
&nbsp;&nbsp;x.report("mike") do |t| t.times do 10.times do |n| mike n end end end  
&nbsp;&nbsp;x.report("ryan") do |t| t.times do 10.times do |n| ryan n end end end  
&nbsp;&nbsp;x.report("ryan2") do |t| t.times do 10.times do |n| ryan2 n end end end

&nbsp;&nbsp;x.compare!  
end

# Calculating -------------------------------------  
# robert 278.388k (+/- 2.8%) i/s - 1.408M in 5.059990s  
# robert2 283.350k (+/- 2.4%) i/s - 1.425M in 5.031756s  
# james 264.979k (+/- 3.3%) i/s - 1.344M in 5.079088s  
# mike 139.618k (+/- 3.8%) i/s - 701.028k in 5.029633s  
# ryan 849.322k (+/- 3.0%) i/s - 4.300M in 5.067430s  
# ryan2 640.050k (+/- 2.5%) i/s - 3.209M in 5.016532s

> **···**
>
> > On Jan 11, 2017, at 03:27, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> #  
> # Comparison:  
> # ryan: 849322.2 i/s  
> # ryan2: 640050.3 i/s - 1.33x slower  
> # robert2: 283349.9 i/s - 3.00x slower  
> # robert: 278387.7 i/s - 3.05x slower  
> # james: 264979.4 i/s - 3.21x slower  
> # mike: 139618.4 i/s - 6.08x slower

---

<div class="post-metadata">

**Author:** ![Daniel\_Ferreira1](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@Daniel\_Ferreira1](https://rubytalk.org/u/Daniel_Ferreira1)\
**Post date:** [11 January 2017 11:48 UTC](https://rubytalk.org/t/case/73994/8 "2017-01-11T11:48:41Z")

</div>

First time I hear about that alias.  
Just awesome! Thank you very much.

> **···**
>
> On Wed, 11 Jan 2017 at 11:27, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On Wed, Jan 11, 2017 at 12:10 AM, James Pacheco \<james.pacheco@gmail.com\> \> wrote:
> > 
> > \>\> a=3
> > 
> > \>\> p case a
> > 
> > \>\> when 0 then 0
> > 
> > \>\> when 1 then 1
> > 
> > \>\> when 2 then 2
> > 
> > \>\> when 3, a\>5 then 3
> > 
> > \>\> else 99
> > 
> > \>\> end
> > 
> > \>
> > 
> > \> a=3
> > 
> > \> p case a
> > 
> > \> when 0 then 0
> > 
> > \> when 1 then 1
> > 
> > \> when 2 then 2
> > 
> > \> when 4..5 then 99 # using a range here to cover your original 'else'
> > 
> > \> clause
> > 
> > \> else 3 # this is now 'when 3, a \> 5'
> > 
> > \> end
> > 
> > And the generic answer for arbitrary conditions is: use a lambda,
> > 
> > because my suggestion to make lambda support #=== as an alias for
> > 
> > #call and # was implemented a long time ago:
> > 
> > irb(main):002:0\> x = lambda {|a| a \> 5}
> > 
> > =\> #\<Proc:0x00000001287250@(irb):2 (lambda)\>
> > 
> > irb(main):003:0\> 10.times {|i| printf "%2d -\> %p\n", i, x === i}
> > 
> > 0 -\> false
> > 
> > 1 -\> false
> > 
> > 2 -\> false
> > 
> > 3 -\> false
> > 
> > 4 -\> false
> > 
> > 5 -\> false
> > 
> > 6 -\> true
> > 
> > 7 -\> true
> > 
> > 8 -\> true
> > 
> > 9 -\> true
> > 
> > =\> 10
> > 
> > Equipped with the knowledge that case uses === for matching you can
> > 
> > easily use that here:
> > 
> > LARGER\_THAN\_5 = lambda {|n| n \> 5}
> > 
> > a=3
> > 
> > p case a
> > 
> > when 0 then 0
> > 
> > when 1 then 1
> > 
> > when 2 then 2
> > 
> > when 3, LARGER\_THAN\_5 then 3
> > 
> > else 99
> > 
> > end
> > 
> > Note, you can rewrite that to
> > 
> > a = 3
> > 
> > p case a
> > 
> > when 0,1,2,3 then a
> > 
> > when LARGER\_THAN\_5 then 3
> > 
> > else 99
> > 
> > end
> > 
> > or use a Hash or ...
> > 
> > irb(main):047:0\> FIXED = {0=\>0,1=\>1,2=\>2,3=\>3}
> > 
> > irb(main):048:0\> -3.upto(10) {|i| printf "%2d -\> %p\n", i,
> > 
> > FIXED.fetch(i) { i\>5 ? 3 : 99 }}
> > 
> > -3 -\> 99
> > 
> > -2 -\> 99
> > 
> > -1 -\> 99
> > 
> > 0 -\> 0
> > 
> > 1 -\> 1
> > 
> > 2 -\> 2
> > 
> > 3 -\> 3
> > 
> > 4 -\> 99
> > 
> > 5 -\> 99
> > 
> > 6 -\> 3
> > 
> > 7 -\> 3
> > 
> > 8 -\> 3
> > 
> > 9 -\> 3
> > 
> > 10 -\> 3
> > 
> > =\> -3
> > 
> > etc.
> > 
> > Cheers
> > 
> > robert
> > 
> > --
> > 
> > [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can
> > 
> > - without end}
> > 
> > [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>
> > 
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> > 
> > That is so cool Robert!

---

<div class="post-metadata">

**Author:** ![Daniel\_Ferreira1](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@Daniel\_Ferreira1](https://rubytalk.org/u/Daniel_Ferreira1)\
**Post date:** [11 January 2017 12:41 UTC](https://rubytalk.org/t/case/73994/9 "2017-01-11T12:41:14Z")

</div>

Thanks for the insight Ryan. I was expecting that. It doesn't take the  
value out of the === alias tough.  
Although I must admite it may decrease code readability. It needs to be  
used in a wise way.

> **···**
>
> On Wed, 11 Jan 2017 at 12:30, Ryan Davis \<ryand-ruby@zenspider.com\> wrote:
> 
> > \> On Jan 11, 2017, at 03:27, Robert Klemme \<shortcutter@googlemail.com\> \> wrote:
> > 
> > \>
> > 
> > \> And the generic answer for arbitrary conditions is: use a lambda,
> > 
> > \> because my suggestion to make lambda support #=== as an alias for
> > 
> > \> #call and # was implemented a long time ago:
> > 
> > As the great Alan Perlis didn't quite say:
> > 
> > \> [Ruby] programmers know the value of everything and the cost of nothing.
> > 
> > Generic answers are great for generic questions, but using a proc in this  
> > case (no pun intended) comes with a very large cost. I really liked James'  
> > suggestion, to flip the logic around, but I knew that the range was
> > 
> > What I'm truly surprised by is James' solution being as slow as it was. I  
> > figured it had to beat out proc activation. I'm also a bit surprised at the  
> > cost of my valueless case (ryan2).
> > 
> > ---- benchmark and results follow
> > 
> > require 'benchmark/ips'
> > 
> > LARGER\_THAN\_5 = lambda { |m| m \> 5}
> > 
> > def robert n
> > 
> > &nbsp;&nbsp;case n
> > 
> > &nbsp;&nbsp;when 0 then 0
> > 
> > &nbsp;&nbsp;when 1 then 1
> > 
> > &nbsp;&nbsp;when 2 then 2
> > 
> > &nbsp;&nbsp;when 3, LARGER\_THAN\_5 then 3
> > 
> > &nbsp;&nbsp;else 99
> > 
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > def robert2 n
> > 
> > &nbsp;&nbsp;case n
> > 
> > &nbsp;&nbsp;when 0,1,2,3 then n
> > 
> > &nbsp;&nbsp;when LARGER\_THAN\_5 then 3
> > 
> > &nbsp;&nbsp;else 99
> > 
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > def mike n
> > 
> > &nbsp;&nbsp;case n
> > 
> > &nbsp;&nbsp;when 0 then 0
> > 
> > &nbsp;&nbsp;when 1 then 1
> > 
> > &nbsp;&nbsp;when 2 then 2
> > 
> > &nbsp;&nbsp;when -\> (x) { x == 3 || x \> 5 } then 3
> > 
> > &nbsp;&nbsp;else 99
> > 
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > def james n
> > 
> > &nbsp;&nbsp;case n
> > 
> > &nbsp;&nbsp;when 0 then 0
> > 
> > &nbsp;&nbsp;when 1 then 1
> > 
> > &nbsp;&nbsp;when 2 then 2
> > 
> > &nbsp;&nbsp;when 4..5 then 99 # using a range here to cover your original 'else'  
> > clause
> > 
> > &nbsp;&nbsp;else 3 # this is now 'when 3, a \> 5'
> > 
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > def ryan n
> > 
> > &nbsp;&nbsp;case n
> > 
> > &nbsp;&nbsp;when 0 then 0
> > 
> > &nbsp;&nbsp;when 1 then 1
> > 
> > &nbsp;&nbsp;when 2 then 2
> > 
> > &nbsp;&nbsp;when 4, 5 then 99 # because range will bite you
> > 
> > &nbsp;&nbsp;else 3
> > 
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > def ryan2 n
> > 
> > &nbsp;&nbsp;case
> > 
> > &nbsp;&nbsp;when n==0 then 0
> > 
> > &nbsp;&nbsp;when n==1 then 1
> > 
> > &nbsp;&nbsp;when n==2 then 2
> > 
> > &nbsp;&nbsp;when n==3 || n\>5 then 3 # just to see the impact of a valueless case
> > 
> > &nbsp;&nbsp;else 99
> > 
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > 10.times do |n|
> > 
> > &nbsp;&nbsp;v0, v1, v2, v3, v4, v5 =
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;robert(n), robert2(n), mike(n), james(n), ryan(n), ryan2(n)
> > 
> > &nbsp;&nbsp;abort "bad" if [v0, v1, v2, v3, v4, v5].uniq.size != 1
> > 
> > end
> > 
> > Benchmark.ips do |x|
> > 
> > &nbsp;&nbsp;x.report("robert") do |t| t.times do 10.times do |n| robert n end end  
> > end
> > 
> > &nbsp;&nbsp;x.report("robert2") do |t| t.times do 10.times do |n| robert2 n end end  
> > end
> > 
> > &nbsp;&nbsp;x.report("james") do |t| t.times do 10.times do |n| james n end end  
> > end
> > 
> > &nbsp;&nbsp;x.report("mike") do |t| t.times do 10.times do |n| mike n end end  
> > end
> > 
> > &nbsp;&nbsp;x.report("ryan") do |t| t.times do 10.times do |n| ryan n end end  
> > end
> > 
> > &nbsp;&nbsp;x.report("ryan2") do |t| t.times do 10.times do |n| ryan2 n end end  
> > end
> > 
> > &nbsp;&nbsp;x.compare!
> > 
> > end
> > 
> > # Calculating -------------------------------------
> > 
> > # robert 278.388k (+/- 2.8%) i/s - 1.408M in  
> > 5.059990s
> > 
> > # robert2 283.350k (+/- 2.4%) i/s - 1.425M in  
> > 5.031756s
> > 
> > # james 264.979k (+/- 3.3%) i/s - 1.344M in  
> > 5.079088s
> > 
> > # mike 139.618k (+/- 3.8%) i/s - 701.028k in  
> > 5.029633s
> > 
> > # ryan 849.322k (+/- 3.0%) i/s - 4.300M in  
> > 5.067430s
> > 
> > # ryan2 640.050k (+/- 2.5%) i/s - 3.209M in  
> > 5.016532s
> > 
> > #
> > 
> > # Comparison:
> > 
> > # ryan: 849322.2 i/s
> > 
> > # ryan2: 640050.3 i/s - 1.33x slower
> > 
> > # robert2: 283349.9 i/s - 3.00x slower
> > 
> > # robert: 278387.7 i/s - 3.05x slower
> > 
> > # james: 264979.4 i/s - 3.21x slower
> > 
> > # mike: 139618.4 i/s - 6.08x slower
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>
> > 
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Daniel\_Ferreira1](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@Daniel\_Ferreira1](https://rubytalk.org/u/Daniel_Ferreira1)\
**Post date:** [11 January 2017 13:22 UTC](https://rubytalk.org/t/case/73994/10 "2017-01-11T13:22:31Z")

</div>

> \> p case a
> 
> \> when 0 then 0
> 
> \> when 1 then 1
> 
> \> when 2 then 2
> 
> \> when 3, a\>5 then 3

> \> else 99
> 
> \> end
> 
> try the else part,
> 
> p case a
> 
> when 0 then 0
> 
> when 1 then 1
> 
> when 2 then 2
> 
> when 3 then 3
> 
> else
> 
> &nbsp;&nbsp;if a \> 5
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3
> 
> &nbsp;&nbsp;else
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;99
> 
> &nbsp;&nbsp;end
> 
> end
> 
> you can replace the if with another (nested) case if you're fond of case :  
> )
> 
> \> Seems you can't combine values and statements (a\>5)
> 
> try the other case and other combinations case w case, case w if, etc,
> 
> p case
> 
> when [0,1,2,3].include?(a)

> What about: (0..3).include?(a)
> 
> &nbsp;&nbsp;&nbsp;a
> 
> when a \> 5
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3
> 
> else
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;99
> 
> end
> 
> kind regards
> 
> --botp
> 
> Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>
> 
> \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

> **···**
>
> On Wed, 11 Jan 2017 at 13:07, botp \<botpena@gmail.com\> wrote:
> 
> > On Wed, Jan 11, 2017 at 6:16 AM, Die Optimisten \> \> \<inform@die-optimisten.net\> wrote:

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 January 2017 13:53 UTC](https://rubytalk.org/t/case/73994/11 "2017-01-11T13:53:13Z")

</div>

> > And the generic answer for arbitrary conditions is: use a lambda,  
> > because my suggestion to make lambda support #=== as an alias for  
> > #call and # was implemented a long time ago:
> 
> As the great Alan Perlis didn't quite say:
> 
> > [Ruby] programmers know the value of everything and the cost of nothing.
> 
> Generic answers are great for generic questions, but using a proc in this case (no pun intended) comes with a very large cost. I really liked James' suggestion, to flip the logic around, but I knew that the range was

I wasn't suggesting to use the lambda here - I was just using this  
case for illustration. For other cases you can actually create pretty  
readable code with this pattern.

> What I'm truly surprised by is James' solution being as slow as it was. I figured it had to beat out proc activation. I'm also a bit surprised at the cost of my valueless case (ryan2).

I believe James' solution creates a new Range instance during every  
iteration (unless there is some optimization going un underneath like  
for Regex) so you could try with the range in a constant.

Kind regards

robert

> **···**
>
> On Wed, Jan 11, 2017 at 1:30 PM, Ryan Davis \<ryand-ruby@zenspider.com\> wrote:
> 
> > > On Jan 11, 2017, at 03:27, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Eric\_Wong](https://avatars.discourse-cdn.com/v4/letter/e/a3d4f5/32.png) [@Eric\_Wong](https://rubytalk.org/u/Eric_Wong)\
**Post date:** [11 January 2017 20:14 UTC](https://rubytalk.org/t/case/73994/12 "2017-01-11T20:14:15Z")

</div>

> What I'm truly surprised by is James' solution being as slow as it was. I figured it had to beat out proc activation. I'm also a bit surprised at the cost of my valueless case (ryan2).

\<snip\>

> def ryan n  
> &nbsp;&nbsp;case n  
> &nbsp;&nbsp;when 0 then 0  
> &nbsp;&nbsp;when 1 then 1  
> &nbsp;&nbsp;when 2 then 2  
> &nbsp;&nbsp;when 4, 5 then 99 # because range will bite you  
> &nbsp;&nbsp;else 3  
> &nbsp;&nbsp;end  
> end

All literals for "when" means MRI/YARV optimizes the above to a  
hash table internally ("opt\_case\_dispatch" in compile.c and insns.def)

> def ryan2 n  
> &nbsp;&nbsp;case  
> &nbsp;&nbsp;when n==0 then 0  
> &nbsp;&nbsp;when n==1 then 1  
> &nbsp;&nbsp;when n==2 then 2  
> &nbsp;&nbsp;when n==3 || n\>5 then 3 # just to see the impact of a valueless case  
> &nbsp;&nbsp;else 99  
> &nbsp;&nbsp;end  
> end

No optimized dispatch, above, since it's all variable.

> **···**
>
> Ryan Davis \<ryand-ruby@zenspider.com\> wrote:
> 
> > # Calculating -------------------------------------  
> > # robert 278.388k (+/- 2.8%) i/s - 1.408M in 5.059990s  
> > # robert2 283.350k (+/- 2.4%) i/s - 1.425M in 5.031756s  
> > # james 264.979k (+/- 3.3%) i/s - 1.344M in 5.079088s  
> > # mike 139.618k (+/- 3.8%) i/s - 701.028k in 5.029633s  
> > # ryan 849.322k (+/- 3.0%) i/s - 4.300M in 5.067430s  
> > # ryan2 640.050k (+/- 2.5%) i/s - 3.209M in 5.016532s  
> > #  
> > # Comparison:  
> > # ryan: 849322.2 i/s  
> > # ryan2: 640050.3 i/s - 1.33x slower  
> > # robert2: 283349.9 i/s - 3.00x slower  
> > # robert: 278387.7 i/s - 3.05x slower  
> > # james: 264979.4 i/s - 3.21x slower  
> > # mike: 139618.4 i/s - 6.08x slower

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [11 January 2017 14:43 UTC](https://rubytalk.org/t/case/73994/13 "2017-01-11T14:43:44Z")

</div>

(0..3) is a range. it will include any numbers (including fractions etc) in bw

(0..3).include? 1.5  
=\> true

kind regards  
--botp

> **···**
>
> On Wed, Jan 11, 2017 at 9:22 PM, Daniel Ferreira \<subtileos@gmail.com\> wrote:
> 
> > > What about: (0..3).include?(a)

---

<div class="post-metadata">

**Author:** ![Eli\_Sadoff](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/eli_sadoff/32/1927_2.png) [@Eli\_Sadoff](https://rubytalk.org/u/Eli_Sadoff)\
**Post date:** [11 January 2017 14:47 UTC](https://rubytalk.org/t/case/73994/14 "2017-01-11T14:47:37Z")

</div>

It's because (a..b).include?(n) checks if n \>= a && n \<= b. It does not  
check if the value is actually inside of the range.

> **···**
>
> On Wed, Jan 11, 2017 at 9:43 AM, botp \<botpena@gmail.com\> wrote:
> 
> > On Wed, Jan 11, 2017 at 9:22 PM, Daniel Ferreira \<subtileos@gmail.com\> \> wrote:  
> > \>\> What about: (0..3).include?(a)
> > 
> > (0..3) is a range. it will include any numbers (including fractions etc)  
> > in bw
> > 
> > (0..3).include? 1.5  
> > =\> true
> > 
> > kind regards  
> > --botp
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Daniel\_Ferreira1](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@Daniel\_Ferreira1](https://rubytalk.org/u/Daniel_Ferreira1)\
**Post date:** [11 January 2017 15:11 UTC](https://rubytalk.org/t/case/73994/15 "2017-01-11T15:11:20Z")

</div>

That is surprising.  
I had this behaviour in my mind:

(0..3).each { |i| puts I }  
0  
1  
2  
3

Different behaviours?

> **···**
>
> On Wed, 11 Jan 2017 at 14:47, Eli Sadoff \<snood1205@gmail.com\> wrote:
> 
> > It's because (a..b).include?(n) checks if n \>= a && n \<= b. It does not  
> > check if the value is actually inside of the range.
> > 
> > On Wed, Jan 11, 2017 at 9:43 AM, botp \<botpena@gmail.com\> wrote:
> > 
> > On Wed, Jan 11, 2017 at 9:22 PM, Daniel Ferreira \<subtileos@gmail.com\> \> wrote:
> > 
> > \>\> What about: (0..3).include?(a)
> > 
> > (0..3) is a range. it will include any numbers (including fractions etc)  
> > in bw
> > 
> > (0..3).include? 1.5
> > 
> > =\> true
> > 
> > kind regards
> > 
> > --botp
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>
> > 
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>
> > 
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Daniel\_Ferreira1](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@Daniel\_Ferreira1](https://rubytalk.org/u/Daniel_Ferreira1)\
**Post date:** [11 January 2017 15:13 UTC](https://rubytalk.org/t/case/73994/16 "2017-01-11T15:13:52Z")

</div>

> That is surprising.  
> I had this behaviour in my mind:
> 
> (0..3).each { |i| puts I }  
> 0  
> 1  
> 2  
> 3
> 
> Sorry about the typo.
> 
> What about the behaviour:  
> \> (0..3).to\_a  
> =\> [0, 1, 2, 3]  
> Different behaviours?
> 
> It's because (a..b).include?(n) checks if n \>= a && n \<= b. It does not  
> check if the value is actually inside of the range.
> 
> \>\> What about: (0..3).include?(a)
> 
> (0..3) is a range. it will include any numbers (including fractions etc)  
> in bw
> 
> (0..3).include? 1.5
> 
> =\> true
> 
> kind regards
> 
> --botp
> 
> Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>
> 
> \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> 
> Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>
> 
> \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

> **···**
>
> On Wed, 11 Jan 2017 at 15:11, Daniel Ferreira \<subtileos@gmail.com\> wrote:
> 
> > On Wed, 11 Jan 2017 at 14:47, Eli Sadoff \<snood1205@gmail.com\> wrote:  
> > On Wed, Jan 11, 2017 at 9:43 AM, botp \<botpena@gmail.com\> wrote:  
> > On Wed, Jan 11, 2017 at 9:22 PM, Daniel Ferreira \<subtileos@gmail.com\> \> wrote:

---

<div class="post-metadata">

**Author:** ![Daniel\_Ferreira1](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@Daniel\_Ferreira1](https://rubytalk.org/u/Daniel_Ferreira1)\
**Post date:** [11 January 2017 15:18 UTC](https://rubytalk.org/t/case/73994/17 "2017-01-11T15:18:31Z")

</div>

> That is surprising.  
> I had this behaviour in my mind:
> 
> (0..3).each { |i| puts I }  
> 0  
> 1  
> 2  
> 3
> 
> Sorry about the typo.
> 
> What about the behaviour:  
> \> (0..3).to\_a  
> =\> [0, 1, 2, 3]
> 
> Different behaviours?
> 
> Also documentation neglects completely that behaviour. A reader will  
> hardly get it.  
> I believe we should update Range#include? documentation with those  
> particular situations.  
> Are there unit tests for that as well?
> 
> It's because (a..b).include?(n) checks if n \>= a && n \<= b. It does not  
> check if the value is actually inside of the range.
> 
> \>\> What about: (0..3).include?(a)
> 
> (0..3) is a range. it will include any numbers (including fractions etc)  
> in bw
> 
> (0..3).include? 1.5
> 
> =\> true
> 
> kind regards
> 
> --botp
> 
> Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>
> 
> \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> 
> Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>
> 
> \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

> **···**
>
> On Wed, 11 Jan 2017 at 15:13, Daniel Ferreira \<subtileos@gmail.com\> wrote:
> 
> > On Wed, 11 Jan 2017 at 15:11, Daniel Ferreira \<subtileos@gmail.com\> wrote:  
> > On Wed, 11 Jan 2017 at 14:47, Eli Sadoff \<snood1205@gmail.com\> wrote:  
> > On Wed, Jan 11, 2017 at 9:43 AM, botp \<botpena@gmail.com\> wrote:  
> > On Wed, Jan 11, 2017 at 9:22 PM, Daniel Ferreira \<subtileos@gmail.com\> \> wrote:

---

<div class="post-metadata">

**Author:** ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)\
**Post date:** [11 January 2017 23:17 UTC](https://rubytalk.org/t/case/73994/18 "2017-01-11T23:17:41Z")

</div>

Hi all!  
Thanks for all these many helpful answers,  
- in 2 days as much conversation as in many months!!  
...don't know when I'm through with all!

Opti :))

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [11 January 2017 17:11 UTC](https://rubytalk.org/t/case/73994/19 "2017-01-11T17:11:48Z")

</div>

> > (0..3).each { |i| puts I }  
> > 0  
> > 1  
> > 2  
> > 3

> > What about the behaviour:  
> > \> (0..3).to\_a  
> > =\> [0, 1, 2, 3]

ruby tries very hard to iterate as much as it can. you would not want  
it to spit out all/infinite numbers.  
eg, if you feed a float for the first obj of the range, ruby will raise,

> (1.0 .. 3).each{|x| p x}

TypeError: can't iterate from Float

if the only the last obj is float, ruby will try to be friendly again.  
eg

> (0 .. 3.5).each{|x| p x}

0  
1  
2  
3

you can also try the #step method.  
eg

> (1.1 .. 2.03).step(0.1).to\_a

=\> [1.1, 1.2000000000000002, 1.3, 1.4000000000000001, 1.5, 1.6,  
1.7000000000000002, 1.8000000000000003, 1.9000000000000001, 2.0]

again, ruby just trying to help programmer.

kind regards  
--botp

> **···**
>
> On Wed, Jan 11, 2017 at 11:13 PM, Daniel Ferreira \<subtileos@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Eli\_Sadoff](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/eli_sadoff/32/1927_2.png) [@Eli\_Sadoff](https://rubytalk.org/u/Eli_Sadoff)\
**Post date:** [11 January 2017 17:13 UTC](https://rubytalk.org/t/case/73994/20 "2017-01-11T17:13:48Z")

</div>

On the whole if the behavior of something seems surprising, it is either  
likely documented or is definitely in the source code.

> **···**
>
> On Wed, Jan 11, 2017 at 12:11 PM, botp \<botpena@gmail.com\> wrote:
> 
> > On Wed, Jan 11, 2017 at 11:13 PM, Daniel Ferreira \<subtileos@gmail.com\> \> wrote:  
> > \>\> (0..3).each { |i| puts I }  
> > \>\> 0  
> > \>\> 1  
> > \>\> 2  
> > \>\> 3
> > 
> > \>\> What about the behaviour:  
> > \>\> \> (0..3).to\_a  
> > \>\> =\> [0, 1, 2, 3]
> > 
> > ruby tries very hard to iterate as much as it can. you would not want  
> > it to spit out all/infinite numbers.  
> > eg, if you feed a float for the first obj of the range, ruby will raise,
> > 
> > \> (1.0 .. 3).each{|x| p x}  
> > TypeError: can't iterate from Float
> > 
> > if the only the last obj is float, ruby will try to be friendly again.  
> > eg
> > 
> > \> (0 .. 3.5).each{|x| p x}  
> > 0  
> > 1  
> > 2  
> > 3
> > 
> > you can also try the #step method.  
> > eg
> > 
> > \> (1.1 .. 2.03).step(0.1).to\_a  
> > =\> [1.1, 1.2000000000000002, 1.3, 1.4000000000000001, 1.5, 1.6,  
> > 1.7000000000000002, 1.8000000000000003, 1.9000000000000001, 2.0]
> > 
> > again, ruby just trying to help programmer.
> > 
> > kind regards  
> > --botp
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
