Unless


#1


So here's what we've got
truth = false (ok)
print "there is no truth" (ok, I defined this variable with that boolean value)
unless truth (unless truth...what? Unless truth = true? I just need someone to please rationalize this to me)


truth = false
print "there is no truth" unless truth


#2

If truth is true, the line will not print.

Think of a height requirement for a ride at the fair...

You can ride the roller coaster unless you are under four feet tall.

#3

so since i defined it as false, how could it end up being true? If later i redefine truth to = true?

does my logic follow in this example:

print "If there is truth, enter true, if not enter false."
user_input = gets.chomp

if user_input = false
print "there is no truth" unless user_input


#4

If true it is not supposed to print.

"You can enter the girls' locker room unless you are a boy."

That line would not be needed. unless is the conditional.


#5

thanks mtf, i'm grasping this now. Unless seems to be somewhat obscure in it's language compared to if statements who's syntax make obvious logical sense


#6

Think in terms of disqualification. unless disqualifies a value.

"You will get an extra week holiday break unless you don't earn your bonus."


#7

that makes sense, so i've got to establish that boolean from the get-go as false in order to later add the conditional, unless, which would allow for the opposite possibility.

I'm wondering if my boolean = true, could i re-state your example as:
you will not get an extra week holiday unless you earn your bonus

I tried it, and it didn't work. So it needs to start off as false?
truth = false
print "there is no truth" unless truth #=> Oops, try again. It looks like you didn't print anything to the console.


#8

The semantics look correct, yes.

x = 0
loop do 
  break unless x < 10
  x = x + 1
end

puts x           # 10

#9

thanks again for all your help!!


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.