convert variable from string to boolean in Ruby
Code snippet on how to convert variable from string to boolean in Ruby
value= gets.chomp
boolean_value= value == 'true'
This code converts a variable from string to boolean in Ruby. First, the gets.chomp
command is used to ask the user to enter one string, which is stored in the value
variable. Then, the value == 'true'
expression is used to check if the entered string is equal to the text true
. The result of this expression is a boolean value, which is stored in the boolean_value
variable. If the entered string is equal to true
, then the boolean_value
variable contains true
; otherwise, the boolean_value
variable contains false
.