Skip to content

Commit

Permalink
added class_super.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthikeyan A K committed Sep 1, 2012
1 parent 9c55a62 commit 541c8c4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions class_super.rb
Original file line number Diff line number Diff line change
@@ -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}"
27 changes: 27 additions & 0 deletions override_methods_super.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 541c8c4

Please sign in to comment.