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
Related
I'm trying to check if the string is a valid phone number in the format
"XXXX XXXX", where every X is an integer from 0 to 9. If its valid return TRUE, else return FALSE.
I'm not sure why !(0..9).include?(char) doesnt work properly.
Any advice would be appreciated.
Thanks
def phone_number(str)
if str.length == 9 && str[4] = " "
str[4] = ""
str.each_char do |char|
if !(0..9).include?(char)
print false
end
end
return true
end
return false
end
print phone_number("1234 5678") # => true
puts
print phone_number("123 5678") # => false
puts
print phone_number("1234 df78") # => false
puts
print phone_number("12345678") # => false
I would advise using a regex to achieve what you want to do !
Here is the example code:
def phone_number(str)
!!str.match(/^\d{4}\s{1}\d{4}$/)
end
print phone_number("1234 5678") # => true
puts
print phone_number("123 5678") # => false
puts
print phone_number("1234 df78") # => false
puts
print phone_number("12345678") # => false
If you for some reason don’t use the regular expression, it’s still possible with plain Ruby:
λ = ->(str) {
str.split(' ').tap do |parts|
break [""] unless parts.size == 2
end.all? do |s|
s.length == 4 && s.delete('[0-9]').empty?
end
}
["1234 5678", "123 5678", "1234 df78", "12345678", "1234"].map(&λ)
#⇒ [true, false, false, false, false]
Here we split the string by parts, check that an amount of parts is exactly 2, both have a length of 4 and consist from digits only.
"0".to_i == 0
also:
"abcdefg".to_i == 0
I want to make sure the string I'm parsing really is just a number (0 included).
Integer("0").zero? rescue false
# => true
Integer("1").zero? rescue false
# => false
Integer("abcdefg").zero? rescue false
# => false
def string_is_integer?(string)
!string.match(/^(\d)+$/).nil? # \d looks for digits
end
def string_is_float?(string)
!string.match(/^(\d)+\.(\d)+$/).nil?
end
def string_is_number?(string)
string_is_integer?(string) || string_is_float?(string)
end
Or, if you don't mind:
def string_is_number?(string)
begin
true if Float(string)
rescue ArgumentError
end || false
end
def is_zero?(string)
(Integer(string) rescue 1).zero?
end
is_zero? "0" #=> true
is_zero? "00000" #=> true
is_zero? "0cat" #=> false
is_zero? "1" #=> false
is_zero? "-0" #=> true
is_zero? "0_000" #=> true
is_zero? "0x00" #=> true
is_zero? "0b00000000" #=> true
Several of these examples illustrate why it's preferable to use Kernel#Integer rather than a regular expression.
First test if the string is integer or not and then match
def is_i?(x)
!!(x =~ /\A[-+]?[0-9]+\z/)
end
def is_zero?(x)
return is_i?(x) && x.to_i == 0
end
# is_zero?("0") will return true
# is_zero?("abcde") will return false
Or you can put these methods in String class like this
class String
def is_i?
!!(self =~ /\A[-+]?[0-9]+\z/)
end
def is_zero?
self.is_i? && self.to_i == 0
end
end
"0".is_zero? # true
"abcde".is_zero? # false
I want to make sure the string I'm parsing really is just a number.
There are two steps involved:
Parsing, i.e. converting the string into a number
Validation, i.e. checking if the string actually is a number
The built-in conversion functions like Integer and Float already perform both steps: they return a numeric result if the string is valid and raise an error otherwise.
Using these functions just for validation and discarding the return value is wasteful. Instead, use the return value and handle the exception, e.g.:
begin
puts 'Enter a number:'
number = Float(gets)
rescue
puts 'Not a valid number'
retry
end
# do something with number
or something like:
def parse_number(string)
Float(string) rescue nil
end
number = parse_number(some_string)
if number
# do something with number
else
# not a number
end
I need a simple function to return "true" or "false" the argument passed to it is:
1 or 0, or true or false
I currently had something like this, so the answer, if possible, should be concise as per below:
def boolean(value); return value ? ( value == 1 ? "true" : "false) : nil; end
Thanks.
Some ideas:
def boolean(x)
%w{1 true}.include?(x).to_s
end
def boolean(x)
(x == '1' || x == 'true').to_s
end
There's also the wannabe bool gem:
require 'wannabe_bool'
'1'.to_b # => true
'0'.to_b # => false
'true'.to_b # => true
'false'.to_b # => false
You might want to have a look how Rails does this typecasting in its database connection adapter:
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
# convert something to a boolean
def value_to_boolean(value)
if value.is_a?(String) && value.empty?
nil
else
TRUE_VALUES.include?(value)
end
end
See: docs for ActiveRecord::ConnectionAdapters::Column
I think this'll work, comments are welcome though:
def b(k); return k ? ( (k == "1" || k == "true") ? "true" : "false" ) : nil; end
puts b("1")
puts b("0")
puts b("true")
puts b("false")
Result:
true
false
true
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.
We can easily convert a keyword into a string:
true.to_s
=> "true"
But how to convert a string into a keyword?
How many keywords do you have? What's your definition of a 'keyword'?
I would implement with a case-command. You may define a to_keyword method for String. My implementation detects true, false, nil (or NULL). The strings are detected, ignoring capitals (TRUE will also be true) Other strings will return a symbol (The string itself would be another reasonable result).
The example can be adapted for further 'keywords' or other results.
class String
#Return 'keyword'
#Detects:
#- true (independend of lower letters/capitals)
#- false (independend of lower letters/capitals)
#- nil/NULL (independend of lower letters/capitals)
def to_keyword
case self
when /\Atrue\Z/i; true
when /\Afalse\Z/i; false
when /\Anil\Z/i, /\ANULL\Z/; nil
else; self.to_sym #return symbol. Other posibility: self.
end
end
end
p 'true'.to_keyword #true
p 'TRUE'.to_keyword #true
p 'false'.to_keyword #false
p 'NULL'.to_keyword #nil (NULL is used in DB like nil)
p 'NULLc'.to_keyword #:NULLc not detected -> symbol
try this:
ruby-1.9.2-p136 :001 > true
=> true
ruby-1.9.2-p136 :002 > eval("true")
=> true
You could try yaml:
require "yaml"
p YAML.load('true')
p YAML.load('TRUE')
p YAML.load('false')
p YAML.load('nil')
p YAML.load('NULL') #nil
I like Knut's answers mostly. I don't think I would support "Null" and others though.
Here is this version which is a little more simple.
class String
def to_keyword
self == "true"
end
end
>> "true".to_keyword
=> true
>> "false".to_keyword
=> false
This problem is pretty straight forward though. In your tests you could simply
:correct => (true_string == "true")
Following your comment, you could do something like that :
true_string = "true"
:correct => !!true_string
# examples
!true_string #=> false
!!true_string #=> true
!!!true_string #=> false
...