imjp
imjp

Reputation: 6695

When to use if..then instead of if

I'm still very new to rails and I was wondering when I should be using if...then instead of if or if it's just a matter of preference.

Thanks in advance.

Upvotes: 0

Views: 118

Answers (2)

bender
bender

Reputation: 1428

Reserved word "then" is used to separate condition from code, as a newline or semicolon, so you can write e.g.

if true then puts "hello" end

That's all...

Upvotes: 1

Chowlett
Chowlett

Reputation: 46677

then is required when the body of an if statement doesn't appear on a new line. So

if today == "Monday" then puts "Boo" end   # then required

if today == "Wednesday"   # then not required
  puts "Meh"
end

if today == "Friday" then   # then allowed
  puts "Yay"
end

That said, a) I'd look slightly oddly at code that followed the last example, and b) the first example can also be written as puts "Boo" if today == "Monday". So, personally, I would probably never use then.

Upvotes: 3

Related Questions