false is not equal to nil, for example:
false == nil # => false
The same goes for false and 0:
false == 0 # => false
You get the same result for nil and 0:
nil == 0 # => false
Why does Ruby act like this?
In Ruby, the only time nil will return true on a == comparison is when you do: nil == nil
You can read more about nil here:
https://ruby-doc.org/core-2.2.3/NilClass.html
nil is the equivalent of undefined in JavaScript or None in Python
Consider in JavaScript:
>> (undefined === false)
=> false
Or Python:
>> (None == False)
=> False
Nil and False (Equality)
Despite what many people new to the language may expect, nil and false are objects rather than language keywords. Consider FalseClass which says:
The global value false is the only instance of class FalseClass and represents a logically false value in boolean expressions.
Likewise, NilClass is:
[t]he class of the singleton object nil.
When you use a method like Comparable#== to compare them, Ruby invokes the spaceship operator <=> on the instances since neither class defines the #== method. Because nil and false are not the same objects, and because neither overrides the basic equality operator, the expression false == nil will always be false.
Likewise, an Integer like 0 is neither an instance of FalseClass nor NilClass. Both false == 0 and nil == 0 are therefore also false because neither instance evaluates to zero.
Truthy and Falsey (Conditionals)
For branching purposes, both false and nil evaluate as false in a conditional expression. Everything else evaluates as true.
Consider the following non-idiomatic, contrived, but hopefully illustrative example of Ruby's basic truth tables:
def truth_table value
if value
true
else
false
end
end
truth_table nil
#=> false
truth_table false
#=> false
truth_table true
#=> true
truth_table 'a'
#=> true
truth_table 0
#=> true
truth_table 1
#=> true
Boolean Evaluation
Nil and false are semantically different in Ruby, and descend from different base classes. They are also not equal to each other. However, they can both be treated as "not true" for branching purposes or within a Boolean context. For example, consider the following idioms for casting a result as a Boolean true or false value:
!(nil)
#=> true
!!(nil)
#=> false
You can use this idiom to shorten the previous truth table example to:
# Convert any value to a Boolean.
def truth_table value
!!value
end
# Test our truth table again in a more compact but less readable way.
table = {}
[nil, false, true, 'a', 0, 1].map { |v| table[v] = truth_table(v) }; table
#=> {nil=>false, false=>false, true=>true, "a"=>true, 0=>true, 1=>true}
I have a value that will be one of four things: boolean true, boolean false, the string "true", or the string "false". I want to convert the string to a boolean if it is a string, otherwise leave it unmodified. In other words:
"true" should become true
"false" should become false
true should stay true
false should stay false
If you use Rails 5, you can do ActiveModel::Type::Boolean.new.cast(value).
In Rails 4.2, use ActiveRecord::Type::Boolean.new.type_cast_from_user(value).
The behavior is slightly different, as in Rails 4.2, the true value and false values are checked. In Rails 5, only false values are checked - unless the values is nil or matches a false value, it is assumed to be true. False values are the same in both versions:
FALSE_VALUES = [false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF"]
Rails 5 Source: https://github.com/rails/rails/blob/5-1-stable/activemodel/lib/active_model/type/boolean.rb
def true?(obj)
obj.to_s.downcase == "true"
end
I've frequently used this pattern to extend the core behavior of Ruby to make it easier to deal with converting arbitrary data types to boolean values, which makes it really easy to deal with varying URL parameters, etc.
class String
def to_boolean
ActiveRecord::Type::Boolean.new.cast(self)
end
end
class NilClass
def to_boolean
false
end
end
class TrueClass
def to_boolean
true
end
def to_i
1
end
end
class FalseClass
def to_boolean
false
end
def to_i
0
end
end
class Integer
def to_boolean
to_s.to_boolean
end
end
So let's say you have a parameter foo which can be:
an integer (0 is false, all others are true)
a true boolean (true/false)
a string ("true", "false", "0", "1", "TRUE", "FALSE")
nil
Instead of using a bunch of conditionals, you can just call foo.to_boolean and it will do the rest of the magic for you.
In Rails, I add this to an initializer named core_ext.rb in nearly all of my projects since this pattern is so common.
## EXAMPLES
nil.to_boolean == false
true.to_boolean == true
false.to_boolean == false
0.to_boolean == false
1.to_boolean == true
99.to_boolean == true
"true".to_boolean == true
"foo".to_boolean == true
"false".to_boolean == false
"TRUE".to_boolean == true
"FALSE".to_boolean == false
"0".to_boolean == false
"1".to_boolean == true
true.to_i == 1
false.to_i == 0
Working in Rails 5
ActiveModel::Type::Boolean.new.cast('t') # => true
ActiveModel::Type::Boolean.new.cast('true') # => true
ActiveModel::Type::Boolean.new.cast(true) # => true
ActiveModel::Type::Boolean.new.cast('1') # => true
ActiveModel::Type::Boolean.new.cast('f') # => false
ActiveModel::Type::Boolean.new.cast('0') # => false
ActiveModel::Type::Boolean.new.cast('false') # => false
ActiveModel::Type::Boolean.new.cast(false) # => false
ActiveModel::Type::Boolean.new.cast(nil) # => nil
Don't think too much:
bool_or_string.to_s == "true"
So,
"true".to_s == "true" #true
"false".to_s == "true" #false
true.to_s == "true" #true
false.to_s == "true" #false
You could also add ".downcase," if you are worried about capital letters.
if value.to_s == 'true'
true
elsif value.to_s == 'false'
false
end
h = { "true"=>true, true=>true, "false"=>false, false=>false }
["true", true, "false", false].map { |e| h[e] }
#=> [true, true, false, false]
In a rails 5.1 app, I use this core extension built on top of ActiveRecord::Type::Boolean. It is working perfectly for me when I deserialize boolean from JSON string.
https://api.rubyonrails.org/classes/ActiveModel/Type/Boolean.html
# app/lib/core_extensions/string.rb
module CoreExtensions
module String
def to_bool
ActiveRecord::Type::Boolean.new.deserialize(downcase.strip)
end
end
end
initialize core extensions
# config/initializers/core_extensions.rb
String.include CoreExtensions::String
rspec
# spec/lib/core_extensions/string_spec.rb
describe CoreExtensions::String do
describe "#to_bool" do
%w[0 f F false FALSE False off OFF Off].each do |falsey_string|
it "converts #{falsey_string} to false" do
expect(falsey_string.to_bool).to eq(false)
end
end
end
end
In Rails I prefer using ActiveModel::Type::Boolean.new.cast(value) as mentioned in other answers here
But when I write plain Ruby lib. then I use a hack where JSON.parse (standard Ruby library) will convert string "true" to true and "false" to false. E.g.:
require 'json'
azure_cli_response = `az group exists --name derrentest` # => "true\n"
JSON.parse(azure_cli_response) # => true
azure_cli_response = `az group exists --name derrentesttt` # => "false\n"
JSON.parse(azure_cli_response) # => false
Example from live application:
require 'json'
if JSON.parse(`az group exists --name derrentest`)
`az group create --name derrentest --location uksouth`
end
confirmed under Ruby 2.5.1
A gem like https://rubygems.org/gems/to_bool can be used, but it can easily be written in one line using a regex or ternary.
regex example:
boolean = (var.to_s =~ /^true$/i) == 0
ternary example:
boolean = var.to_s.eql?('true') ? true : false
The advantage to the regex method is that regular expressions are flexible and can match a wide variety of patterns. For example, if you suspect that var could be any of "True", "False", 'T', 'F', 't', or 'f', then you can modify the regex:
boolean = (var.to_s =~ /^[Tt].*$/i) == 0
Although I like the hash approach (I've used it in the past for similar stuff), given that you only really care about matching truthy values - since - everything else is false - you can check for inclusion in an array:
value = [true, 'true'].include?(value)
or if other values could be deemed truthy:
value = [1, true, '1', 'true'].include?(value)
you'd have to do other stuff if your original value might be mixed case:
value = value.to_s.downcase == 'true'
but again, for your specific description of your problem, you could get away with that last example as your solution.
I have a little hack for this one. JSON.parse('false') will return false and JSON.parse('true') will return true. But this doesn't work with JSON.parse(true || false). So, if you use something like JSON.parse(your_value.to_s) it should achieve your goal in a simple but hacky way.
Rubocop suggested format:
YOUR_VALUE.to_s.casecmp('true').zero?
https://www.rubydoc.info/gems/rubocop/0.42.0/RuboCop/Cop/Performance/Casecmp
In rails, I've previously done something like this:
class ApplicationController < ActionController::Base
# ...
private def bool_from(value)
!!ActiveRecord::Type::Boolean.new.type_cast_from_database(value)
end
helper_method :bool_from
# ...
end
Which is nice if you're trying to match your boolean strings comparisons in the same manner as rails would for your database.
You could just add !! before the variable:
!!test_string
Close to what is already posted, but without the redundant parameter:
class String
def true?
self.to_s.downcase == "true"
end
end
usage:
do_stuff = "true"
if do_stuff.true?
#do stuff
end
This function works for any input:
def true?(value)
![false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF"].include? value
end
then you have:
true?(param) #returns true or false
I am supposed to get a string and make sure that it represents a binary number like "101011".
This question deals with hexadecimal strings, but I do not know how I should replace the letter H in the regex in statement !str[/\H/]. Could you help me?
A straightforward way using regex is:
"101011" !~ /[^01]/
Four ways using String methods:
str1 = '100100110'
str2 = '100100210'
String#delete
str1.delete('01') == '' #=> true
str2.delete('01') == '' #=> false
String#tr
str1.tr('01','') == '' #=> true
str2.tr('01','') == '' #=> false
String#gsub
str1.gsub(/[01]/,'') == '' #=> true
str2.gsub(/[01]/,'') == '' #=> false
String#count
str1.count('01') == str1.size #=> true
str2.count('01') == str2.size #=> false
Here is one way of doing this:
"101011".chars.all? {|x| x =~ /[01]/} # true if binary, else false
Recently I started learning Ruby. I am practicing logical operators in irb and I got these results, which I don't understand. Can you please clarify these examples for me?
1 and 0
#=> 0
0 and 1
#=> 1
0 && 1
#=> 1
As opposed to other languages like C, in Ruby all values except for nil and false are considered “truthy”. This means, that all these values behave like true in the context of a boolean expression.
Ruby's boolean operators will not return true or false. Instead, they return the first operand that causes the evaluation of the condition to be complete (also known as short-circuit evaluation). For boolean and that means, it will either return the first “falsy” operand or the last one:
false && 1 # => false (falsy)
nil && 1 # => nil (falsy)
false && nil # => false (falsy)
1 && 2 # => 2 (truthy)
For boolean or that means, it will either return the first “truthy” operand or the last one:
false || 1 # => 1 (truthy)
nil || 1 # => 1 (truthy)
false || nil # => nil (falsy)
1 || 2 # => 1 (truthy)
This allows for some interesting constructs. It is a very common pattern to use || to set default values, for example:
def hello(name)
name = name || 'generic humanoid'
puts "Hello, #{name}!"
end
hello(nil) # Hello, generic humanoid!
hello('Bob') # Hello, Bob!
Another similar way to acheive the same thing is
name || (name = 'generic humanoid')
With the added benefit that if name is truthy, no assignment is performed at all. There is even a shortcut for this assignment of default values:
name ||= 'generic humanoid'
If you paid careful attention you will have noticed that this may cause some trouble, if one valid value is false:
destroy_humans = nil
destroy_humans ||= true
destroy_humans
#=> true
destroy_humans = false
destroy_humans ||= true
destroy_humans
#=> true, OMG run!
This is rarely the desired effect. So if you know that the values can only be a String or nil, using || and ||= is fine. If the variable can be false, you have to be more verbose:
destroy_humans = nil
destroy_humans = true if destroy_humans.nil?
destroy_humans
#=> true
destroy_humans = false
destroy_humans = true if destroy_humans.nil?
destroy_humans
#=> false, extinction of humanity digressed!
That was close! But wait, there is another caveat – specifically with the usage of and and or. These should never be used for boolean expressions, because they have very low operator precedence. That means they will be evaluated last. Consider the following examples:
is_human = true
is_zombie = false
destroy_human = is_human && is_zombie
destroy_human
#=> false
is_human = true
is_zombie = false
destroy_human = is_human and is_zombie
destroy_human
#=> true, Waaaah but I'm not a zombie!
Let me add some parentheses to clarify what's happening here:
destroy_human = is_human && is_zombie
# equivalent to
destroy_human = (is_human && is_zombie)
destroy_human = is_human and is_zombie
# equivalent to
(destroy_human = is_human) and is_zombie
So and and or are really just useful as “control-flow operators”, for example:
join_roboparty or fail 'forever alone :('
# this will raise a RuntimeError when join_roboparty returns a falsy value
join_roboparty and puts 'robotz party hard :)'
# this will only output the message if join_roboparty returns a truthy value
I hope that clarifies everything you need to know about these operators. It takes a bit of getting used to, because it differs from the way other languages handle it. But once you know how to use the different options, you've got some powerful tools at hand.
Both values are 'truthy' (in Ruby everything that isn't nil or false is truthy), so in all cases the second value is returned. On the contrary, if you use 'or', first value will be returned:
1 || 0 #=> 1
0 || 1 #=> 0
In Ruby both 0 and 1 is truth value. (Only nil and false are false value)
If both operands are truth value, and, && returns the last value.
Why does precedence behave differently in the following code :
a = true or true and false
if a then
puts "Foo"
end
if true or true and false then
puts "Quux"
end
This only prints "Foo" and not "Quux".
E:\Home>ruby --version
ruby 1.9.3p392 (2013-02-22) [i386-mingw32]
E:\Home>ruby test.rb
Foo
The operator = has precedence over and and or (see there for instance). So in your first statement,
a = true
is evaluated before the rest. Hence, you set a to true.
Look at the operator precedence
http://www.techotopia.com/index.php/Ruby_Operator_Precedence
The order of evaluation is like this
(((a = true) or true) and false)
# a = true
if (true or true) and false then
# equivalent to
if true and false then
To get a more natural behaviour, use && and ||.
see here
2 and 7
#=> 7
2 or 7
#=> 2
2 or 7 and 12
#=> 12
2 or 7 or 12
#=> 2
2 or 7 and 12 or 10
#=> 12
true and true and false
#=> false
true or true or false
#=> true
true and true or false
#=> true
true or true and false
#=> false
true or true and false and true
#=> false
Summary :
(a) when you will use in an expression with only and operator value of the expression is always the last operand and the reverse is true for expression having only or operator.In case of Boolean the full expression will be evaluated.
(b) When an expression will have and and or mixed, evaluation will continue till the last and and its ROH operand will be the value of the expression(in case of Boolean last and RHO,LHO will be evaluated,after that result will be produced as Boolean operation rule.
Applying the rule of (b)
below code works:
if true or true and false then
puts "Quux"
end
#=> nil
a = true or true and false
#=> false
if a then
puts "Foo"
end
#Foo
#=> nil
Below code outputs Foo due to the precedence of = over and,or.
The expression a = true or true and false evaluates as follows applying the rule (b)
a = true or true and false
||
(a = true) or true and false
||
true and false
||
false
One more good application of rule (b)
if 2 and 3 or nil
p "hi"
end
#=> "hi"
if 2 or 3 and nil
p "hello"
end
#nothing printed