Mega Code Archive

 
Categories / Ruby / Class
 

Creating an Abstract Class and Method

class Shape2D   def initialize     raise NotImplementedError.       new("#{self.class.name} is an abstract class.")   end   def area     raise NotImplementedError.       new("#{self.class.name}#area is an abstract method.")   end end class Square < Shape2D   def initialize(length)     @length = length   end   def area     @length ** 2   end end Square.new(10).area                              # => 100 # NotImplementedError: Shape2D is an abstract class.