-
Notifications
You must be signed in to change notification settings - Fork 1
Example: Interface
Andrias Meisyal edited this page Jul 16, 2017
·
2 revisions
An interface is a contract of what the classes can do. When a class implements an interface, it promises to provide implementation to all the abstract methods declared in the interface. An interface contains only public methods without implementation and possibly constants. For example, Shape
class is an interface that provide methods to calculate area
and perimeter
of shapes. There is Circle
class implements those methods. Circle
class has only radius
attribute.
If you generate code from diagram above, the extension will generate two separated .rb
files. One file is an interface which is generated as a Ruby module.
shape.rb
module Shape
def area
# TODO(person name): Implement this method here.
end
def perimeter
# TODO(person name): Implement this method here.
end
end
circle.rb
require_relative 'shape.rb'
class Circle
include Shape
def initialize(radius)
@radius = radius
end
private
attr_accessor :radius
def to_s
"Your string representation of the object will be written here."
end
end
Note that the example use default configuration of the extension.