# What is the difference "case && if"?

**URL:** <https://www.ruby-forum.com/t/what-is-the-difference-case-if/90502>\
**Category:** Rails\
**Created:** [March 29, 2007, 11:01am UTC](https://www.ruby-forum.com/t/what-is-the-difference-case-if/90502 "2007-03-29T11:01:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 29, 2007, 11:01am UTC](https://www.ruby-forum.com/t/what-is-the-difference-case-if/90502/1 "2007-03-29T11:01:31Z")

</div>

Hello there,

Sometime I feel like I understood Ruby and sometime I think its too  
freaky???

Here is example what I don’t get?

This work as it is…

if request.post?  
#do something

When I try to put it inside case, it doesn’t work???

case request  
when post  
#doesn’t work  
when post?  
#doesn’t work  
end

This is confusing??

Regards,  
Jamal

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 29, 2007, 11:13am UTC](https://www.ruby-forum.com/t/what-is-the-difference-case-if/90502/2 "2007-03-29T11:13:47Z")

</div>

The statements you are making are not the same

> if request.post?  
> is either true or false

> case request  
> when post  
> is akin to saying:  
> if request == post

If you want to use a case statement, then try something like:  
case request.post?  
when true

# do something

when false  
…etc.

There’s not much need for a case statement when evaluating true or  
false, though.

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Toby Privett  
\<\< [rorbar.com](http://rorbar.com) \>\>

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 29, 2007, 11:20am UTC](https://www.ruby-forum.com/t/what-is-the-difference-case-if/90502/3 "2007-03-29T11:20:48Z")

</div>

The case statement compares the target of the case (request) with each  
when item (post, post?) using ===.

So what you’ve done there is compared the request object to something  
called post. I would think that that would be a no method error…

But anyway, try

case request.method  
when :post  
# do something  
when :get  
# do something else  
end

That’s untested and off the cuff however…

b

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 29, 2007, 11:29am UTC](https://www.ruby-forum.com/t/what-is-the-difference-case-if/90502/4 "2007-03-29T11:29:13Z")

</div>

Ben M. wrote:

> case request.method  
> when :post  
> # do something  
> when :get  
> # do something else  
> end
> 
> That’s untested and off the cuff however…

Thanks for that, I figure it out when I posted this topic

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 29, 2007, 11:30am UTC](https://www.ruby-forum.com/t/what-is-the-difference-case-if/90502/5 "2007-03-29T11:30:44Z")

</div>

toby privett wrote:

> The statements you are making are not the same
> 
> > if request.post?  
> > is either true or false
> 
> > case request  
> > when post  
> > is akin to saying:  
> > if request == post
> 
> If you want to use a case statement, then try something like:  
> case request.post?  
> when true
> 
> # do something
> 
> when false

Thanks, this help me to understand the difference…

So request.post? is either (true or false)

and request.method == post  
check if method post, get or put is used etc 😃
