Jean-Luc
Jean-Luc

Reputation: 3613

Ruby override class method

Is it possible to, for example, override the 'puts' method and replace it with a function that calls 'puts' twice?

For example:

class IO
  def puts str
    puts str
    puts str
  end
end

I know this example is stupid but I need it for something else. Basically, I want to override a function but I want the overriding function to include calls to the original, overridden function.

Upvotes: 1

Views: 1173

Answers (1)

user904990
user904990

Reputation:

class IO

  alias puts_orig puts
  def puts str
    puts_orig str
    puts_orig str
  end
end

Upvotes: 5

Related Questions