コンテンツにスキップ

式として

if の値は、それぞれのブランチで見つかった最後の式の値です。

a = if 2 > 1
      3
    else
      4
    end
a # => 3

if ブランチが空の場合、または存在しない場合は、nil が含まれているとみなされます。

if 1 > 2
  3
end

# The above is the same as:
if 1 > 2
  3
else
  nil
end

# Another example:
if 1 > 2
else
  3
end

# The above is the same as:
if 1 > 2
  nil
else
  3
end