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
6 个解决方案
#1
52
inflection_point = (firm.inflection_point ? 1 : 0)
#2
15
If you just have that at one point, then rudolph9's answer is 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 0
and 1
, but I guess you are dealing with some inputs or outputs from/to some external system that deals with 0
and 1
. Then, doing like this will make sense.
在Ruby中,你应该保持所有的逻辑处理真值而不是0和1,但我猜你正在处理来自/处理0和1的一些外部系统的一些输入或输出。然后,这样做将会合理。
#3
7
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
#4
5
In Ruby, if
is an expression. There's no need to assign to a variable inside the then
and else
branches, just return the value you want and assign the variable to the result of the if expression
:
在Ruby中,if是一个表达式。无需在then和else分支中分配变量,只需返回所需的值并将变量分配给if表达式的结果:
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
#5
3
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)
基本上是指,如果第一个条件的计算结果为true(firm.inflection_point),则返回“?”之后的值。 (1)否则,返回“:”之后的值(0)
#6
2
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个字符。
#1
52
inflection_point = (firm.inflection_point ? 1 : 0)
#2
15
If you just have that at one point, then rudolph9's answer is 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 0
and 1
, but I guess you are dealing with some inputs or outputs from/to some external system that deals with 0
and 1
. Then, doing like this will make sense.
在Ruby中,你应该保持所有的逻辑处理真值而不是0和1,但我猜你正在处理来自/处理0和1的一些外部系统的一些输入或输出。然后,这样做将会合理。
#3
7
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
#4
5
In Ruby, if
is an expression. There's no need to assign to a variable inside the then
and else
branches, just return the value you want and assign the variable to the result of the if expression
:
在Ruby中,if是一个表达式。无需在then和else分支中分配变量,只需返回所需的值并将变量分配给if表达式的结果:
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
#5
3
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)
基本上是指,如果第一个条件的计算结果为true(firm.inflection_point),则返回“?”之后的值。 (1)否则,返回“:”之后的值(0)
#6
2
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个字符。