Ruby: Conditionals
Ruby is Cool
Conditionals, if/else statements and all that… Compared to other languages, Ruby has a couple of things that make it way-cool in regard to conditionals, and this is to do with its more real-person-non-roboty-human-type syntax. Let’s look at a basic conditional, an if/else statement:
So nothing too unexpected with a regular if/else statement then. As you can see it is very similar to any other language’s equivalent. But Ruby has another trick, the Unless statement. When thinking of our code syntactically like a sentence, as Ruby allows us, sometimes it might be more applicable or easier to understand when we use “unless” instead of “if”. Take a decisive step in the direction of your choosing.
Wait, Ruby is Ice Cold
Even c-o-o-l-er is that instead of writing these large sections of code as above, an if/else or unless statment in Ruby can be written in one line, like so:
print "it's true" if true
or
print "it's false" unless true
Well, flip.