not sure I understand your question correctly, but Ruby does not offer
boolean data type as in many other programming languages. However,
there are “true” and “false” objects which you can use for that
purpose:
v = true # or v = false
if v then
puts “v is true”
else
puts “v is false”
end
not sure I understand your question correctly, but Ruby does not offer
boolean data type as in many other programming languages.
I want to hear this Thanks!
I myself patched class String and added a .to_bool method that turns
“false” into false and “true” into true.
Why ruby core team don’t want to add it to ruby lang?
I have some trouble. How can i define the binary of value in ruby? (I
have a value, and I want to know that’s it true or false).
You don’t really need to. Everything in ruby has a “truthiness” to it
and can be used in logical expressions. Only nil and false are “falsey”
and everything else are “truthy”. In other words, just use objects and
they’ll take care of you.
if var then
do_something
end
If you feel REALLY insecure about typing in a dynamic language you can
always double-negate:
class Object
def truthy?
!!self
end
end
if var.truthy? then # wasted cycles for no gain
do_something
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.