Q:

How to write nested if/then statements in Ruby

I'm supposed to 

define a method, three_digit_format(n), that accepts an integer, n, as an argument. Assume that n < 1000. Your method should return a string version of n, but with leading zeros such that the string is always 3 characters long.

I have been tinkering with versions of the below code, but I always get errors.  Can anyone advise?
def three_digit_format(n)
  stringed = n.to_s
  stringed.size
  if stringed.size > 2
    return stringed
  end
  elsif stringed > 1
    return "0" + stringed
  end
  else 
    return "00" + stringed
  end
end
puts three_digit_format(9)

A:

rjust
You could just use rjust:
n.to_s.rjust(3, '0')

If integer is greater than the length of str, returns a new String of
  length integer with str right justified and padded with padstr;
  otherwise, returns str.

Your code
Problem
If you let your text editor indents your code, you can notice there's something wrong:
def three_digit_format(n)
  stringed = n.to_s
  stringed.size
  if stringed.size > 2
    return stringed
  end
elsif stringed > 1       # <- elsif shouldn't be here
  return "0" + stringed
end
  else
    return "00" + stringed
  end
end
puts three_digit_format(9)

Solution
if, elsif and else belong to the same expression : there should only be one end at the end of the expression, not for each statement.
def three_digit_format(n)
  stringed = n.to_s
  if stringed.size > 2
    return stringed
  elsif stringed.size > 1
    return "0" + stringed
  else
    return "00" + stringed
  end
end
puts three_digit_format(9)
# 009

