Ruby - Class Override methods

Introduction

You can override your own methods.

Demo

class Dog 
  def talk # from  w  ww. jav a  2 s  .com
    puts "Woof!" 
  end 
end 

my_dog = Dog.new 
my_dog.talk 

class Dog 
  def talk 
    puts "Howl!" 
  end 
end 

my_dog.talk

Result

Here, you created a basic class with a simple method, then reopened that class and redefined a method.

Related Topic