It's the never ending "end"s that bother me about Ruby. class Mess def chaos(x) if x > 0 [1,2,3].each do |i| case i when 1 if i.odd? puts "odd" else puts "even" end when 2 begin puts "trying" rescue puts "failed" end else puts "other" end end else puts "negative" end end end Clear away all those ends and the program logic pops out. Much fresher! class Mess: def chaos(self, x): if x > 0: for i in [1, 2, 3]: match i: c…
class Mess
OUTPUTS = {
1 => ->(i) { i.odd? ? "odd" : "even" },
2 => ->(_) { "trying" },
3 => ->(_) { "other" }
}.freeze
def chaos(x)
return puts "negative" unless x.positive?
[1, 2, 3].each do |i|
puts OUTPUTS[i]&.call(i) || "other"
end
end
end