Tutorial by Examples

Let's say we have two classes, Cat and Dog. class Cat def eat die unless has_food? self.food_amount -= 1 self.hungry = false end def sound puts "Meow" end end class Dog def eat die unless has_food? self.food_amount -= 1 self.hungry = f...
Multiple inheritance is a feature that allows one class to inherit from multiple classes(i.e., more than one parent). Ruby does not support multiple inheritance. It only supports single-inheritance (i.e. class can have only one parent), but you can use composition to build more complex classes using...
Inheritance allows classes to define specific behaviour based on an existing class. class Animal def say_hello 'Meep!' end def eat 'Yumm!' end end class Dog < Animal def say_hello 'Woof!' end end spot = Dog.new spot.say_hello # 'Woof!' spot.eat ...
Mixins are a beautiful way to achieve something similar to multiple inheritance. It allows us to inherit or rather include methods defined in a module into a class. These methods can be included as either instance or class methods. The below example depicts this design. module SampleModule def...
Methods are inherited class A def boo; p 'boo' end end class B < A; end b = B.new b.boo # => 'boo' Class methods are inherited class A def self.boo; p 'boo' end end class B < A; end p B.boo # => 'boo' Constants are inherited class A WOO = 1 end class ...

Page 1 of 1