feezy26 feezy26 - 2 months ago 6
Ruby Question

How do I center all of my puts statements in a program without applying the center method to each string?

So I have this code:

puts "hey".center(150)
puts "yo".center(150)
puts "sup".center(150)


How do I make this shorter and more concise? I was thinking of editing the original puts method?

Answer

You can just define a method like this:

def my_puts(str)
  puts str.center(150)
end

And use it like:

my_puts "hey"
my_puts("yo")
Comments