diff --git a/class_super.rb b/class_super.rb new file mode 100644 index 0000000..f47ae81 --- /dev/null +++ b/class_super.rb @@ -0,0 +1,27 @@ +#!/usr/bin/ruby +# class_super.rb + +class Rectangle + + def set_dimension length, breadth + @length, @breadth = length, breadth + end + + def area + @length * @breadth + end + +end + + +class Square < Rectangle + + def set_dimension side_length + super side_length, side_length + end + +end + +square = Square.new +square.set_dim 7 +puts "Area: #{square.area}" diff --git a/override_methods_super.rb b/override_methods_super.rb new file mode 100644 index 0000000..7551ff0 --- /dev/null +++ b/override_methods_super.rb @@ -0,0 +1,27 @@ +#!/usr/bin/ruby +# override_methods.rb + +class A + def belongs_to + puts "I belong to in class A" + end + + def another_method + puts "Just another method in class A" + end +end + +class B < A + def another_method + super + puts "Just another method in class B" + end +end + + +a = A.new +b = B.new +a.belongs_to +a.another_method +b.belongs_to # This is not overriden so method in class A is called +b.another_method # This is overridden so method in class B is called