Write in Ruby with more logic