Skip to content

Commit

Permalink
Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
msatmod committed Aug 30, 2022
1 parent 73fc416 commit 18e5d20
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Day 15/Inheritance-1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Inheritance in Ruby

class Person

attr_accessor:name, :age, :trait

end

class Sportsman < Person

attr_accessor:sports

end

sp = Sportsman.new

sp.age = 20
sp.name = "Filly"
sp.sports = "Basketball"

puts sp.inspect
26 changes: 26 additions & 0 deletions Day 15/Inheritance-2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Inheritance in Ruby

class Animal

attr_accessor:color, :name

end

class Tiger < Animal

def speak
return "Grrrr....!!"

end

end

tiger = Tiger.new

tiger.color = "Blue"

tiger.name = "Filly"

puts tiger.inspect

puts tiger.speak
30 changes: 30 additions & 0 deletions Day 15/Inheritance-3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Inheritance in Ruby

class Animal

attr_accessor:color, :name

def initialize(name, color)

@name = name

@color = color

end

end

class Tiger < Animal

def speak
return "Grrrr....!!"

end

end

tiger = Tiger.new("Filly", "Blue")

puts tiger.inspect

puts tiger.speak
30 changes: 30 additions & 0 deletions Day 15/Inheritance-4.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Inheritance in Ruby

class Animal

attr_accessor:color, :name

end

class Tiger < Animal

def initialize(name, color)

@name = name

@color = color

end

def speak
return "Grrrr....!!"

end

end

tiger = Tiger.new("Filly", "Blue")

puts tiger.inspect

puts tiger.speak
44 changes: 44 additions & 0 deletions Day 15/Inheritance-5.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Inheritance in Ruby

class Animal

attr_accessor:color, :name

def identify

return "My name is #{@name} and I am from #{self.class}"

end

end

class Tiger < Animal

def initialize(name, color)

@name = name

@color = color

end

def speak
return "Grrrr....!!"

end

end

tiger = Tiger.new("Filly", "Blue")

animal = Animal.new

puts tiger.inspect

puts tiger.speak

puts tiger.identify

puts animal.inspect

puts animal.identify
38 changes: 38 additions & 0 deletions Day 15/Inheritance-6_Method_Overriding.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Method Overriding - Inheritance in Ruby

class ParentArea

# Constructor
def initialize(w, h)

@width = w
@height = h

end

# To calculate the area from Parent Class
def getArea

return "Area from Parent class is #{@width*@height}"

end

end

class ChildArea < ParentArea

# To calculate the area from Child Class
def getArea

puts super()

return "Area from Child class is #{@width*@height}"

end

end

childobj = ChildArea.new(10, 20)

puts childobj.getArea

0 comments on commit 18e5d20

Please sign in to comment.