# Case (variable) when type1 ... when type2 ... end

**URL:** <https://www.ruby-forum.com/t/case-variable-when-type1-when-type2-end/194092>\
**Category:** Ruby\
**Created:** [August 30, 2010, 8:40am UTC](https://www.ruby-forum.com/t/case-variable-when-type1-when-type2-end/194092 "2010-08-30T08:40:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ebca7d/32.png) [@eugen](https://www.ruby-forum.com/u/eugen)\
**Post date:** [August 30, 2010, 8:40am UTC](https://www.ruby-forum.com/t/case-variable-when-type1-when-type2-end/194092/1 "2010-08-30T08:40:05Z")

</div>

Hi all,  
how can I write case statement to check of specific type the variable  
has?  
Something like:

case var  
when :Fixnum  
puts “#{var} is Fixnum”  
when :String  
puts “#{var} is String”  
end

I tried this also

case var.class  
when ‘Fixnum’  
puts “#{var} is Fixnum”  
when ‘String’  
puts “#{var} is String”  
end

but ruby refuses to understand me ☹  
What is the best way to write conditional that depends of variable type?

Thank you!

---

<div class="post-metadata">

**Author:** ![eugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ebca7d/32.png) [@eugen](https://www.ruby-forum.com/u/eugen)\
**Post date:** [August 30, 2010, 10:34am UTC](https://www.ruby-forum.com/t/case-variable-when-type1-when-type2-end/194092/2 "2010-08-30T10:34:13Z")

</div>

> how can I write case statement to check of specific type the variable has?  
> Something like:

It simpler:

> case var  
> when :Fixnum  
> puts “#{var} is Fixnum”  
> when :String  
> puts “#{var} is String”  
> end

case var  
when Fixnum  
puts “#{var} is Fixnum”  
when String  
puts “#{var} is String”  
end

(no “:”)

Cheers,

---

<div class="post-metadata">

**Author:** ![eugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ebca7d/32.png) [@eugen](https://www.ruby-forum.com/u/eugen)\
**Post date:** [August 30, 2010, 10:52am UTC](https://www.ruby-forum.com/t/case-variable-when-type1-when-type2-end/194092/3 "2010-08-30T10:52:22Z")

</div>

oh, I was almost there 😛  
Thank you !

---

<div class="post-metadata">

**Author:** ![eugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ebca7d/32.png) [@eugen](https://www.ruby-forum.com/u/eugen)\
**Post date:** [August 30, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/case-variable-when-type1-when-type2-end/194092/4 "2010-08-30T13:01:45Z")

</div>

Eugen,

Monday, August 30, 2010, 12:39:36 AM, you wrote:

EC\> Hi all,  
EC\> how can I write case statement to check of specific type the  
variable has?  
EC\> Something like:

EC\> case var  
EC\> when :Fixnum  
EC\> puts “#{var} is Fixnum”  
EC\> when :String  
EC\> puts “#{var} is String”  
EC\> end

EC\> I tried this also

EC\> case var.class  
EC\> when ‘Fixnum’  
EC\> puts “#{var} is Fixnum”  
EC\> when ‘String’  
EC\> puts “#{var} is String”  
EC\> end

EC\> but ruby refuses to understand me ☹  
EC\> What is the best way to write conditional that depends of variable  
type?

EC\> Thank you!

You were very close.

case var  
when Fixnum  
puts “#{var} is Fixnum”  
when String  
puts “#{var} is String”  
end

Ralph

---

<div class="post-metadata">

**Author:** ![eugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ebca7d/32.png) [@eugen](https://www.ruby-forum.com/u/eugen)\
**Post date:** [September 3, 2010, 7:26am UTC](https://www.ruby-forum.com/t/case-variable-when-type1-when-type2-end/194092/5 "2010-09-03T07:26:02Z")

</div>

On 08/30/10 10:33, Jean-Julien F. wrote:

> > end  
> > Cheers,

Hi, could you explain me why this works? It’s a special behaviour of the  
“case” statment?

---

<div class="post-metadata">

**Author:** ![eugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ebca7d/32.png) [@eugen](https://www.ruby-forum.com/u/eugen)\
**Post date:** [September 3, 2010, 9:08am UTC](https://www.ruby-forum.com/t/case-variable-when-type1-when-type2-end/194092/6 "2010-09-03T09:08:28Z")

</div>

No, it’s not a special behavior. Explanation is here.

[http://ruby.about.com/od/beginningruby/qt/On-Case-And-Class.htm](http://ruby.about.com/od/beginningruby/qt/On-Case-And-Class.htm)

---

<div class="post-metadata">

**Author:** ![eugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ebca7d/32.png) [@eugen](https://www.ruby-forum.com/u/eugen)\
**Post date:** [September 3, 2010, 9:10am UTC](https://www.ruby-forum.com/t/case-variable-when-type1-when-type2-end/194092/7 "2010-09-03T09:10:49Z")

</div>

On Fri, Sep 3, 2010 at 7:25 AM, tilde [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > puts “#{var} is Fixnum”
> > 
> > (no “:”)
> > 
> > Cheers,
> 
> Hi, could you explain me why this works? It’s a special behaviour of the  
> “case” statment?

“case” uses operator “===” to check conditions. If you do “case x  
when y” then “y === x” is invoked to check the condition (note the  
order of x and y). You can even cook your own

positive = lambda {|x| x \>= 0}  
class \<\< positive  
alias :=== :call  
end

(-4…4).each do |i|  
case i  
when positive  
puts “#{i} pos”  
else  
puts “#{i} neg”  
end  
end

btw, there is also another form of case

case # no value here!  
when i \>= 0  
puts “#{i} pos”  
else  
puts “#{i} neg”  
end

Kind regards

robert
