开发者

When to use if..then instead of if

开发者 https://www.devze.com 2023-03-23 09:12 出处:网络
I\'m still very new to rails and I was wondering when I should be using if...then 开发者_JAVA百科instead of if or if it\'s just a matter of preference.

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

Thanks in advance.


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.


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消