How do I convert boolean values to integers?

后端 未结 7 1067
日久生厌
日久生厌 2020-12-16 10:39

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?

if firm.inflection_point
  inflectio         


        
相关标签:
7条回答
  • 2020-12-16 11:14

    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:

    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.

    0 讨论(0)
提交回复
热议问题