Forum: Ruby on Rails Simple problem of negating boolean in params-hash

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.
478ad84559379887af8637fb025835d4?d=identicon&s=25 Adrian D (Guest)
on 2009-06-02 00:05
(Received via mailing list)
Hi,

I have a simple problem. I get a boolean value in the parameter hash
and want to negate it. But somehow I missed there a fundamental
concept, because that doesn't make any sense to me:

Code:

test1 = !params[:editmode]
test2 = params[:editmode]

logger.info("Editmode: #{params[:editmode]} Test 1: #{test1} und Test
2: #{test2}")

Output:

"Editmode: false Test 1: false und Test 2: false"

or when editmode is true: "Editmode: true Test 1: false und Test 2:
true"

Thanks for your help
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-06-02 00:08
(Received via mailing list)
On Jun 1, 6:49 pm, Adrian D <Adrian.Debbe...@googlemail.com> wrote:
> Hi,
>
> I have a simple problem. I get a boolean value in the parameter hash
> and want to negate it. But somehow I missed there a fundamental
> concept, because that doesn't make any sense to me:
>
> Code:
>
> test1 = !params[:editmode]
> test2 = params[:editmode]

All controller parameters are strings, so this negation doesn't do
what you think it does (because strings are not nil and they are not
the boolean value false, any string always has the logical value true,
and its negation the logical value false). You need to compare the
parameters with whatever string you have decided represents true/false

Fred
This topic is locked and can not be replied to.