Forum: Ruby on Rails what is the difference "case && if" ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-03-29 11:01
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
B3bcedc723a39bd0395f3c68cbca49e4?d=identicon&s=25 toby privett (Guest)
on 2007-03-29 11:13
(Received via mailing list)
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/.
>
> >
>


--
Toby Privett
<< rorbar.com >>
8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-03-29 11:20
(Received via mailing list)
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
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-03-29 11:29
Ben Munat 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
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-03-29 11:30
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 :D
This topic is locked and can not be replied to.