Skip to content

Instantly share code, notes, and snippets.

@moh-alsheikh
Last active May 18, 2022 06:13
Show Gist options
  • Save moh-alsheikh/932bdbde4bf1c026eae2dc883f8127e8 to your computer and use it in GitHub Desktop.
Save moh-alsheikh/932bdbde4bf1c026eae2dc883f8127e8 to your computer and use it in GitHub Desktop.
class Architecture
def roof(architecture)
architecture.roof
end
end
class Tudor
def desc
puts "Tudor architecture originated in England in the 1500s, shaped by new innovations like chimney stacks and fireplaces and the increasing affordability of brick.\n\n"
end
def roof
puts "Steep roof"
end
end
class Craftsman
def desc
puts "The Craftsman style is one of the most distinctive movements in American architecture. In the 1890s, a group of influential Boston architects and interior designers organized to promote its principles, drawing inspiration from Britain’s Arts and Crafts movement.\n\n"
end
def roof
puts "Low pitched roof \nWide overhanging eaves"
end
end
architecture = Architecture.new
architecture.roof(Tudor.new)
architecture.roof(Craftsman.new)
class Architecture
def desc
puts "Architecture is the craft of planning, designing, and constructing buildings and other physical structures\n\n"
end
def roof; end;
end
class Tudor < Architecture
def desc
puts "Tudor architecture originated in England in the 1500s, shaped by new innovations like chimney stacks and fireplaces and the increasing affordability of brick.\n\n"
end
def roof
puts "Steep roof"
end
end
class Craftsman < Architecture
def desc
puts "The Craftsman style is one of the most distinctive movements in American architecture. In the 1890s, a group of influential Boston architects and interior designers organized to promote its principles, drawing inspiration from Britain’s Arts and Crafts movement.\n\n"
end
def roof
puts "Low pitched roof \nWide overhanging eaves"
end
end
tudor = Tudor.new
tudor.roof
craftsman = Craftsman.new
craftsman.roof

Definition

  • Polymorphism is a made up of two words Poly which means Many and Morph which means Forms.

  • Multiple classes with the same method but with different implementation.

  • Implement many different implementations of the same method.

  • Classes have different functionality but they share common interference.

  • in Ruby, it means being able to send the same message to different objects and get different results.

Let’s look at a few different ways to achieve this.

Implement polymorphism

We can implement polymorphism using :

  1. Polymorphism using Inheritance

  2. Polymorphism using Duck-Typing

Misc links

https://thoughtbot.com/blog/back-to-basics-polymorphism-and-ruby

https://www.geeksforgeeks.org/polymorphism-in-ruby/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment