0

I want to know if I can create a variable with a boolean as value, something like this:

my_var =
if X > Y
   print "The value is true" 
else
   print "The value is false"
end

This is so I can write a boolean easily.

EDIT:

I'm trying to do "shortcuts" using variables that are equal to a boolean. That way, if I require to write a boolean multiple times in a code, I can just "call" the variable.

Example:

x = gets.chomp
y = gets.chomp

required_boolean = 
if x > y
puts "x is greater than y"
elsif x = Y
puts "X and Y are equal"
else
puts "y is greater than x"
end

puts "Since x has a value of #{x} and y has a value of {y}, we can say that #{required_boolean}"
4
  • I edited the code, but not sure where to put my_var.. it is confusing. Please edit and format the code again correctly Commented Mar 12, 2016 at 20:03
  • 1
    Not clear what you are trying to do. Commented Mar 12, 2016 at 20:14
  • 1
    Perhaps you want something like this: my_var = X > Y (which will be true or false), then my_var ? (print "The value is true") : (print "The value is false") or print "The value is #{my_var ? 'true' : 'false'}". Commented Mar 12, 2016 at 20:22
  • 1
    Actually, true.to_s == "true", you could run: "The value is #{(my_var = (x > y) ).to_s}" Commented Mar 12, 2016 at 21:25

1 Answer 1

1

boolean in ruby supports #to_s..

Which means you can do something like this:

true.to_s # => "true"
false.to_s # => "false"
(1 < 2).to_s # => "true"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.