turn into one-liner: `def con(n) return 'pg' if (n % 105).zero? return 'plg' if (n % 15).zero? n.to_s end` in ruby

You can turn the given code into a one-liner by using the ternary operator. Here's the one-liner version:

main.rb
def con(n)
   (n % 105).zero? ? 'pg' : ((n % 15).zero? ? 'plg' : n.to_s)
end
77 chars
4 lines

The ternary operator is used to conditionally return different values based on the given conditions. In this case, if the number n is divisible by 105, it returns 'pg', if it's divisible by 15, it returns 'plg', and otherwise, it returns n converted to a string.

This one-liner code simplifies the original function while maintaining the same logic.

related categories

gistlibby LogSnag