Code Editor
The Zen of Ruby

A Simpler 'If'

You've seen the Ruby if statement before:

if condition # Do something! end

If the "do something" is a short, simple expression, however, we can move it up into a single line (as you saw in the last exercise). The syntax looks like this:

expression if boolean

Ruby will expect an expression followed by if followed by a boolean. The order is important. You can do this:

puts "It's true!" if true

but not this:

if true puts "It's true!"

It's also important to note that you don't need an end when you write your if statement all on one line.

Report a Bug
If you see a bug or any other issue with this page, please report it here.