Last active
December 19, 2015 21:09
-
-
Save rtoal/6018421 to your computer and use it in GitHub Desktop.
A trivial Ruby circle class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Circle | |
attr_reader :r | |
def initialize(x = 0, y = 0, r = 1) | |
@x = x | |
@y = y | |
@r = r | |
end | |
def center | |
[@x, @y] | |
end | |
def area | |
Math::PI * @r * @r | |
end | |
def perimeter | |
Math::PI * 2.0 * @r | |
end | |
def expand!(factor) | |
@r *= factor | |
self | |
end | |
def move!(dx, dy) | |
@x += dx | |
@y += dy | |
self | |
end | |
def to_s | |
"<Circle at (#{@x},#{@y}) with radius #{@r}>" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment