How to convert String to Boolean in Ruby?
Last Updated :
27 Mar, 2024
In this article, we will learn how to convert string to Boolean in Ruby using various methods. Let’s understand each of them with the help of examples.
Converting string to Boolean using String#casecmp
casecmp
method
compares two strings while ignoring the case and returns 0 if they are equal, -1 if the receiver precedes the argument, and 1 if the receiver follows the argument alphabetically.
Syntax:
string.casecmp("true").zero?
Example: In this example we compare the given string with "true" while ignoring case. If the strings are equal, it returns 0. We use this property to check if the result of casecmp
is zero to determine if the string represents true.
Ruby
def convert_to_boolean_using_casecmp(string)
string.casecmp("true").zero?
end
# Test examples
string1 = "True"
string2 = "false"
string3 = "Yes"
puts "String: #{string1}, Boolean Value: #{convert_to_boolean_using_casecmp(string1)}" # Output: true
puts "String: #{string2}, Boolean Value: #{convert_to_boolean_using_casecmp(string2)}" # Output: false
puts "String: #{string3}, Boolean Value: #{convert_to_boolean_using_casecmp(string3)}" # Output: true
OutputString: True, Boolean Value: true
String: false, Boolean Value: false
String: Yes, Boolean Value: false
Converting string to Boolean using String#downcase
String#downcase converts the string to lowercase and then we compare it with "true" to check if it represents true..
Syntax:
string.downcase == "true"
Example: In this example we convert the given string to lowercase using downcase and then compares it with "true". If the lowercase string matches "true", it returns true.
Ruby
def convert_to_boolean_using_downcase(string)
string.downcase == "true"
end
# Test examples
string1 = "True"
string2 = "false"
string3 = "Yes"
puts "String: #{string1}, Boolean Value: #{convert_to_boolean_using_downcase(string1)}" # Output: true
puts "String: #{string2}, Boolean Value: #{convert_to_boolean_using_downcase(string2)}" # Output: false
puts "String: #{string3}, Boolean Value: #{convert_to_boolean_using_downcase(string3)}" # Output: true
OutputString: True, Boolean Value: true
String: false, Boolean Value: false
String: Yes, Boolean Value: false
Convert string to Boolean using Regular Expressions
Regular expressions can be employed to match specific patterns in strings, such as "true" or "false", and then convert them to Boolean values.
Syntax:
!!(string =~ /^(true|t|yes|y)$/i)
Example: In this example we uses a regular expression /^(true|t|yes|y)$/i
to match strings like "true", "t", "yes", or "y" regardless of case. The expression !!(string =~ /^(true|t|yes|y)$/i)
returns true if the string matches the pattern, otherwise false.
Ruby
def convert_to_boolean_using_regex(string)
!!(string =~ /^(true|t|yes|y)$/i)
end
# Test examples
string1 = "True"
string2 = "false"
string3 = "Yes"
puts "String: #{string1}, Boolean Value: #{convert_to_boolean_using_regex(string1)}" # Output: true
puts "String: #{string2}, Boolean Value: #{convert_to_boolean_using_regex(string2)}" # Output: false
puts "String: #{string3}, Boolean Value: #{convert_to_boolean_using_regex(string3)}" # Output: true
OutputString: True, Boolean Value: true
String: false, Boolean Value: false
String: Yes, Boolean Value: true
Similar Reads
How to convert string to boolean in PHP? Given a string and the task is to convert given string to its boolean. Use filter_var() function to convert string to boolean value. Examples: Input : $boolStrVar1 = filter_var('true', FILTER_VALIDATE_BOOLEAN); Output : true Input : $boolStrVar5 = filter_var('false', FILTER_VALIDATE_BOOLEAN); Output
2 min read
How to Convert String to JSON in Ruby? In this article, we will learn how to convert a string to JSON in Ruby. String to JSON conversion consists of parsing a JSON-formatted string into a corresponding JSON object, enabling structured data manipulation and interoperability within Ruby applications. Converting String to JSON in RubyBelow
2 min read
How to convert String to Hash in Ruby? In this article, we will discuss how to convert string to hash in ruby. We can convert a string to a hash through different methods ranging from parsing JSON and parsing YAML to custom string parsing. Table of Content Converting string to hash using JSON parsingConverting string to hash using YAML p
2 min read
How to Convert String to Boolean in TypeScript ? In Typescript, sometimes you receive the data as strings but need to work with boolean values or identify the boolean equivalent of it.There are several approaches to convert string to boolean in TypeScript which are as follows:Table of ContentUsing Conditional StatementUsing JSON.parse() MethodUsin
4 min read
How to Convert a String to Lower or Upper Case in Ruby? In this article, we will discuss how to convert a string to lower or upper case in Ruby. We can convert a string to lower or upper case using built-in methods provided in the Ruby language. Table of Content Using DowncaseUsing UpcaseUsing CapitalizeUsing DowncaseThe downcase method is used to conver
2 min read
Convert Boolean To String In Pandas Dataframe Pandas, a powerful data manipulation library in Python, offers multiple ways to convert boolean values to strings within a DataFrame. In this article, we will see how to convert boolean to String in Pandas DataFrame in Python. Python Convert Boolean To String In Pandas DataframeBelow are some exampl
3 min read