eme*_*ery 89 ruby string boolean type-conversion
我有一个值将是四件事之一:布尔值true,布尔值false,字符串"true"或字符串"false".我想将字符串转换为布尔值,如果它是一个字符串,否则保持不变.换一种说法:
"真实"应该成真
"假"应该变成虚假
真的应该保持真实
假应该保持虚假
ste*_*lag 112
def true?(obj)
obj.to_s.downcase == "true"
end
Run Code Online (Sandbox Code Playgroud)
rad*_*ado 92
如果你使用Rails 5,你可以做到ActiveModel::Type::Boolean.new.cast(value)
.
在Rails 4.2中,使用 ActiveRecord::Type::Boolean.new.type_cast_from_user(value)
.
行为略有不同,如在Rails 4.2中,检查true值和false值.在Rails 5中,只检查错误值 - 除非值为nil或匹配false值,否则假定为true.两个版本中的假值相同:
FALSE_VALUES = [false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF"]
Rails 5来源:https://github.com/rails/rails/blob/5-1-stable/activemodel/lib/active_model/type/boolean.rb
Ste*_*aig 20
我经常使用这种模式来扩展Ruby的核心行为,以便更容易处理将任意数据类型转换为布尔值,这使得处理不同的URL参数等变得非常容易.
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
Run Code Online (Sandbox Code Playgroud)
所以假设你有一个参数foo
可以是:
你可以随叫foo.to_boolean
而不是使用一堆条件,它将为你完成其余的魔法.
在Rails中,我将它添加到core_ext.rb
几乎所有项目中命名的初始化程序中,因为这种模式非常常见.
## 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
Run Code Online (Sandbox Code Playgroud)
Dav*_*ley 17
不要想太多:
bool_or_string.to_s == "true"
Run Code Online (Sandbox Code Playgroud)
所以,
"true".to_s == "true" #true
"false".to_s == "true" #false
true.to_s == "true" #true
false.to_s == "true" #false
Run Code Online (Sandbox Code Playgroud)
如果您担心大写字母,也可以添加".downcase".
arc*_*ana 15
if value.to_s == 'true'
true
elsif value.to_s == 'false'
false
end
Run Code Online (Sandbox Code Playgroud)
Car*_*and 13
h = { "true"=>true, true=>true, "false"=>false, false=>false }
["true", true, "false", false].map { |e| h[e] }
#=> [true, true, false, false]
Run Code Online (Sandbox Code Playgroud)
Jig*_*att 13
在 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
Run Code Online (Sandbox Code Playgroud)
在 rails 5.1 应用程序中,我使用构建在ActiveRecord::Type::Boolean
. 当我从 JSON 字符串反序列化布尔值时,它对我来说非常有效。
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
Run Code Online (Sandbox Code Playgroud)
初始化核心扩展
# config/initializers/core_extensions.rb
String.include CoreExtensions::String
Run Code Online (Sandbox Code Playgroud)
规格
# 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
Run Code Online (Sandbox Code Playgroud)
小智 5
我对此有一个小窍门。JSON.parse('false')
将返回false
并将JSON.parse('true')
返回true。但这不适用于JSON.parse(true || false)
. 因此,如果您使用类似的东西,JSON.parse(your_value.to_s)
应该以一种简单但黑客的方式实现您的目标。
在Rails中,我更喜欢使用ActiveModel::Type::Boolean.new.cast(value)
此处其他答案中提到的方法
但是当我写普通的Ruby lib时。然后我使用一个hack JSON.parse
(标准Ruby库)将字符串“ true”转换为true
和“ false”转换为false
。例如:
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
Run Code Online (Sandbox Code Playgroud)
实时应用程序中的示例:
require 'json'
if JSON.parse(`az group exists --name derrentest`)
`az group create --name derrentest --location uksouth`
end
Run Code Online (Sandbox Code Playgroud)
在Ruby 2.5.1中得到确认
归档时间: |
|
查看次数: |
100628 次 |
最近记录: |