goldylucks
goldylucks

Reputation: 6144

how to monkey patch the puts method in ruby

I want to wrap the puts method with new lines before and after.

I searched the docs and didn't find where this function comes from. Ideas?

Upvotes: 1

Views: 206

Answers (1)

coalest
coalest

Reputation: 71

You could override the puts method like so:

def puts(object)
  super('')
  super(object)
  super('')
end

If you really wanted to you could monkey patch the method like below. Although your changes would probably have unintended effects.

module Kernel
  def puts(object)
    # code
  end
end

You can read about monkey patching in the Ruby docs here.

Upvotes: 2

Related Questions