Learn

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.

Instructions

1.

Give it a go! Type a one-line if statement in the editor to the right. It can be whatever you like.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?