Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)
Unless
When we use if, we're asking Ruby if something is true. If we wanted to ask it if something is not true we could reverse the order of our if/else statements. This would surely be confusing. Luckily Ruby supplies us with the unless statement.
cost = 10
unless cost > 15
puts "I'll buy it."
else
puts "That's too much."
end
⇩
I'll buy it.
Make the cost higher than 15 now.
cost = 22
unless cost > 15
puts "I'll buy it."
else
puts "That's too much."
end
⇩
That's too much.
One more example.
num = 7
unless num.even?
puts "That number is odd."
else
puts "That number is even."
end
⇩
That number is odd.
When we use even? or odd? a true or false is returned depending on whether the integer you're asking about is in fact even or odd. We set our variable num to the odd number seven. Then we said, "Unless num is even puts that the number is odd."
Gets
We want to take our user input and later insert it into a string.
puts "How are you feeling today?"
my_feeling = gets
puts my_feeling
⇩
"How are you feeling today?"
You'll now notice that you're prompted to type something in the command line. You need to, "Type your feelings Luke, you know them to be true." then press enter. Here's what I typed:
good
⇩
good
We set the variable my_feeling to what we typed in. That is what gets allows us to do.