ruby 如何将布尔值转换为整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13537206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:32:16  来源:igfitidea点击:

How do I convert boolean values to integers?

rubyboolean

提问by marcamillion

I have a boolean value to check if it is true, then set a local variable. How do I refactor this so it is more Ruby-ish?

我有一个布尔值来检查它是否为真,然后设置一个局部变量。我如何重构它以使其更像 Ruby-ish?

if firm.inflection_point
  inflection_point = 1
else
  inflection_point = 0
end

回答by rudolph9

inflection_point = (firm.inflection_point ? 1 : 0)

回答by sawa

If you just have that at one point, then rudolph9's answeris good, but if you are having a similar kind of logic all over the place, then maybe it might make sense with general use in mind to monkey patch:

如果您只是在某一时刻拥有它,那么rudolph9 的答案是好的,但是如果您到处都有类似的逻辑,那么考虑到猴子补丁的一般用途,这可能是有意义的:

class FalseClass; def to_i; 0 end end
class TrueClass; def to_i; 1 end end

inflection_point = firm.inflection_point.to_i

Within Ruby, you should keep all of your logic dealing with truth values rather than 0and 1, but I guess you are dealing with some inputs or outputs from/to some external system that deals with 0and 1. Then, doing like this will make sense.

在 Ruby 中,您应该让所有逻辑处理真值而不是0and 1,但我猜您正在处理来自/到某个处理0and 的外部系统的一些输入或输出1。那么,这样做是有意义的。

回答by Andre Figueiredo

Another alternative is use of short-circuit operators:

另一种选择是使用短路运算符:

inflection_point && 1 || 0


irb(main):001:0> true && 1 || 0
=> 1
irb(main):002:0> false && 1 || 0
=> 0

回答by J?rg W Mittag

In Ruby, ifis an expression. There's no need to assign to a variable inside the thenand elsebranches, just return the value you want and assign the variable to the result of the if expression:

在 Ruby 中,if是一个表达式。不需要在thenandelse分支内部分配变量,只需返回您想要的值并将变量分配给 的结果if expression

inflection_point = if firm.inflection_point
  1
else
  0
end

In simple cases like this, it's more readable to write the entire expression on a single line:

在像这样的简单情况下,将整个表达式写在一行上会更易读:

inflection_point = if firm.inflection_point then 1 else 0 end

You can also use the conditional operator, which I personally find to be much less readable:

您还可以使用条件运算符,我个人认为它的可读性要差得多:

inflection_point = firm.inflection_point ? 1 : 0

回答by thebugfinder

What you need is a conditional operation that is known as Ternary Operator It's used in almost every language and it uses the symbols ? and :

您需要的是一种称为三元运算符的条件运算。它几乎在所有语言中都有使用,并且使用符号?和 :

inflection_point = firm.inflection_point ? 1 : 0

basically means, if the first condition evaluates to true (firm.inflection_point), return the value after "?" (1) otherwise, return the value after ":" (0)

基本上意味着,如果第一个条件评估为真(firm.inflection_point),则返回“?”之后的值。(1) 否则,返回“:”后的值 (0)

回答by Darth Egregious

Here's another method:

这是另一种方法:

5 - bool.to_s.length

5 - bool.to_s.length

This takes advantage of the fact that 'true'has four characters, while 'false'has 5.

这利用了'true'有四个字符而'false'有 5个字符的事实。

回答by Miko?aj Wawrzyniak

It is not pure ruby solution but, You can use ActiveRecord::Type::Integer.new.cast(true)

它不是纯红宝石解决方案,但是,您可以使用 ActiveRecord::Type::Integer.new.cast(true)