Skip to content

Instantly share code, notes, and snippets.

@Dickyrdiar
Created August 29, 2023 09:16
Show Gist options
  • Save Dickyrdiar/c71edc109691db225a1b7b0761b40fb8 to your computer and use it in GitHub Desktop.
Save Dickyrdiar/c71edc109691db225a1b7b0761b40fb8 to your computer and use it in GitHub Desktop.
pholymorphism example
class Rectangle < Shape
attr_accessor :width, :height
def initialize(width, height)
@width = width
@height = height
end
def area
@width * @height
end
end
class Circle < Shape
attr_accessor :radius
def initialize(radius)
@radius = radius
end
def area
Math::PI * @radius**2
end
end
shapes = [Rectangle.new(5, 10), Circle.new(3)]
shapes.each do |shape|
puts "Area: #{shape.area}"
e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment