How to Override a Ruby Class
The Ruby programming language uses classes, which is a type of object-oriented programming that uses inheritance. You override a Ruby class by creating a class that inherits the parent class and contains functions with the same name as the parent class. This means that when you instantiate the class and call the class function, the overridden class is ignored.
Instructions
-
-
1
Open your Ruby on Rails editor from the Windows program menu. Open your Ruby Web project you want to edit.
-
2
Double-click the Ruby class file you want to edit. You create the override class after the parent class and specify the class inheritance in its definition.
-
-
3
Create the inherited class. The following code creates a class named "Dog" that inherits from a class named "Animals":
class Dog < Animals
end
You place the override functions within this class definition.
-
4
Override the parent class. For instance, if the Animals parent class has a function named "Fur" you override that class function by creating a class function named "Fur" in the "Dog" class. The following code shows you how to override a class:
class Dog < Animals
def Fur
puts 'Dogs have shaggy fur'
end
end
-
1