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