Smoothing methods
You can, for example, use your old method as old_greetwell as override with yours:
class Greetings
class << self
alias_method :old_greet, :greet
def greet(message)
(message == "ge") ? "A Very Good Evening" : old_greet(message)
end
end
end
and then you can:
puts Greetings.greet("ge")
Method chain
Using the Rails Function alias_method_chain:
class Greetings
class << self
def greet_with_modifications(message)
(message == "ge") ? "A Very Good Evening" : greet_without_modifications(message)
end
alias_method_chain :greet, :modifications
end
end
and then you can:
puts Greetings.greet("ge")
Class extension
You can create your own class that extends the original as follows:
module My
class Greetings < ::Greetings
def self.greet(message)
case message
when "ge"
"A Very Good Evening"
else
super(message)
end
end
end
end
and then you can:
puts My::Greetings.greet("ge")