-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Karthikeyan A K
committed
Sep 1, 2012
1 parent
9c55a62
commit 541c8c4
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |