Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a param is true or false?

This is really racking my brain, but maybe I'm trying to hard.

I'm passing a param via a URL (example.com?debug=true)

So I basically want to say:

if params[:debug] == true  do xyz else  do abc end 

But for whatever reason that if statement just isn't doing like it seems like it should.

Is there a better way to do that if/else statement based on a param being true or false?

The debug param will either have a value of true, no value, or a value of false (as far as the URL goes).

like image 767
Shpigford Avatar asked Nov 10 '09 22:11

Shpigford


People also ask

Is false == false true?

False == (False or True) In this expression python would first solve the comparison operator in bracket. => (False or True) is said to be True. Because the 'OR' operator search for the first truthy value, and if found it returns the value else 'False'. Now we have: =>False == True is False.

How do you check if something is true or false in Python?

You can check if a value is either truthy or falsy with the built-in bool() function. According to the Python Documentation, this function: Returns a Boolean value, i.e. one of True or False . x (the argument) is converted using the standard truth testing procedure.


2 Answers

params come in as strings, so you need to compare against "true", not true.

like image 152
sepp2k Avatar answered Sep 27 '22 17:09

sepp2k


You could use ActiveRecord's method of checking truthful values if you don't want to reinvent the wheel (this is what is used when passing params inside an ActiveRecord object

Rails 3-4.1

if ActiveRecord::ConnectionAdapters::Column.value_to_boolean(params[:debug])     do xyz else     do abc 

Rails 4.2.0

ActiveRecord::Type::Boolean.new.type_cast_from_database(params[:debug]) 

Rails 5

ActiveModel::Type::Boolean.new.cast(params[:debug]) 

Might be worth wrapping in a helper but never the less it's quite flexible:

rails c Loading development environment (Rails 3.2.6) 1.9.3p194 :001 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean '1'  => true  1.9.3p194 :002 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean '0'  => false  1.9.3p194 :003 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean 1  => true  1.9.3p194 :004 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean true  => true  1.9.3p194 :005 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean 'true'  => true  1.9.3p194 :006 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean 'on'  => true  1.9.3p194 :007 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean 'off' 

Custom extension

Some people frown on extending core classes but this does fit with the DRY principle.

# config/initializer/boolean.rb class Boolean   def self.parse(value)     ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)   end end 

Then used like

if Boolean.parse(params[:debug])   then xyz 
like image 27
toxaq Avatar answered Sep 27 '22 19:09

toxaq