Ruby way to define binary

hi2all

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).

The simplest (but not good in my opinion) way is to do it:

v = value.to_s
return v == “false” | v == “true”

What method I can use? I can’t find something helpful in core.

Thanks, Michael

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).

Use match

ruby-1.9.2-p290 :001 > value = “road”
=> “road”
ruby-1.9.2-p290 :002 > if value.match(‘road’)
ruby-1.9.2-p290 :003?> puts “true”
ruby-1.9.2-p290 :004?> else
ruby-1.9.2-p290 :005 > puts “false”
ruby-1.9.2-p290 :006?> end
true
=> nil
ruby-1.9.2-p290 :007 >

Wayne

On Wed, Dec 7, 2011 at 4:05 AM, Misha O. [email protected] wrote:

What method I can use? I can’t find something helpful in core.

Thanks, Michael


Posted via http://www.ruby-forum.com/.

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

On 07.12.11 09:56, Marc H. wrote:

I myself patched class String and added a .to_bool method that turns
“false” into false and “true” into true.

Why would you ever need to represent a false as the string ‘false’?

k

I myself patched class String and added a .to_bool method that turns
“false” into false and “true” into true.

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 :wink: 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?

Cheers, Michael

On Dec 6, 2011, at 12:05 , Misha O. wrote:

hi2all

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