Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Convert If Statements to Case

Is it possible to use a case statement to replace these if statements?

if (a%3 == 0) then puts "%3"
elsif (a%4 == 0) then puts "%4"
elsif (a%7 == 0 && a%13 == 0) then puts "%%"
like image 345
Verhogen Avatar asked Jul 23 '26 04:07

Verhogen


1 Answers

case
  when (a % 3).zero? then puts "%3"
  when (a % 4).zero? then puts "%4"
  when (a % 7).zero? && (a % 13).zero? then puts "%%"
end
like image 84
Simone Carletti Avatar answered Jul 28 '26 14:07

Simone Carletti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!