Skip to content

Instantly share code, notes, and snippets.

@rtoal
Last active December 19, 2015 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtoal/6018421 to your computer and use it in GitHub Desktop.
Save rtoal/6018421 to your computer and use it in GitHub Desktop.
A trivial Ruby circle class
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