One line if statement in Ruby

2020-04-30 03:20发布

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条回答
爷、活的狠高调
2楼-- · 2020-04-30 03:39
(day -= 31; month = "April") if day > 31

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

day, month = day - 31, "April" if day > 31
查看更多
登录 后发表回答