0

In Ruby, I know that control flow statements typically follow the following pattern.

If statement
  do something
else
  do something else
end

From my experiences that was the only pattern I noticed. However, when looking at code on a website, I saw something new that looked similar (if-then). I've never seen that before and would appreciate any explanations.

if statement then
  do something
else
  do something else
end

I'd like to know what the difference between those code blocks is. They seem to do the same thing.

Edit: I would like to clarify, I'm talking about an if then line followed by a block, not if then on one line. Please keep that in mind

sawa
  • 165,429
  • 45
  • 277
  • 381
  • Looked at that, didn't get my answer. This is different. In my example, I have the then statement on the same line as the if statement FOLLOWED by a block. I don't have the if/then statement on the same line. Thanks for your help though –  May 23 '14 at 02:16
  • I encourage you to reread the link because it discusses *exactly that* at great length. – roippi May 23 '14 at 02:17
  • So what I gather is then is optional? –  May 23 '14 at 02:20

1 Answers1

2

The "then" syntax is used when you want to use the "if" expression on one line. In this case there needs to be a separator to ensure the line is understood by the interpreter.

See here Ruby If Syntax