One line if statement in Ruby

2020-04-30 03:29发布

问题:

I have following piece of code:

if day > 31 
  day -= 31 
  month = "April"
end

Can I write it in one line different than:

if day > 31 then day -= 31 and month = "April" end

?

I've tried it like:

if day > 31 {day -= 31; month = "April"} 

But it doesn't work

回答1:

(day -= 31; month = "April") if day > 31

Alternate way (As suggested by @mudasobwa in comments below) :

day, month = day - 31, "April" if day > 31