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...
精彩评论