Why should you avoid the then keyword in Ruby?

后端 未结 3 372
甜味超标
甜味超标 2021-01-18 00:42

It\'s mentioned in several Ruby style guides that you should \"Never use then.\" Personally, I think the \"then\" keyword allows you to make code denser, which tends to be

3条回答
  •  死守一世寂寞
    2021-01-18 01:12

    If I remember correctly, then is just one of the delimiters to separate the condition from the true part (semicolon and newline being the others)

    if you have an if statement that is a one-liner, you'd have to use one of the delimiters.

    if (1==2) then puts "Math doesn't work" else puts "Math works!" end

    for multi-line ifs, then is optional (newline works)

    if (1==2) 
        puts "Math doesn't work" 
    else 
        puts "Math works!" 
    end
    

    Could you post a link to one of the style-guides that you mention...

提交回复
热议问题