If statements/Booleans


#1

Hey guys!
So I’m working on this lesson here…
https://www.codecademy.com/courses/learn-ruby/lessons/control-flow-in-ruby/exercises/unless?action=resume_content_item

and I thought it’d be cool to add more to it by trying to take someone’s input converting it into a boolean and based on whether it’s true or false provide the correct output (I hope that made sense :slight_smile:) anyway here’s my code:

print "Are you hungry?"
hunger = gets.chomp.downcase

if hunger == "yes"
** then hungry = true**


** else hungry = false**


**end if **


** unless hungry**
** puts “Go Ruby!”**
** else**
** puts “Time to eat!”**
end

Basically if a person were to type “Yes or yes”, it would make hungry = true and output “Time to eat!”
and if they type anything else hungry would be false and it’d output “Go Ruby!” Unfortunately, I can’t seem to figure out how to do that. I’m guessing it’s a process that I don’t know about yet but any feedback helps.

I realize I’m going past the lesson here but I really wanted to experiment and try out new things.


#2

That looks like it’s roughly right, but if there are any details slightly wrong then they are kind of drowned out in the formatting issues. Code should be shared in an exact manner, if I were to copy it to a file I should end up with an identical file as you…otherwise it’s not the same code

To debug it I suggest looking at how it behaves differently from what you expect. That usually says a lot about which code is wrong. If there’s an error message, then that says a lot about the issue as well.

One thing I’d do differently, but behaves the same, is to assign hunger to the result of the comparison because that’s a boolean - rather than using an if-expression to assign true if the result is true and false if the result is false (see how it’s a bit redundant? assign to the result instead)