Skip to content

Instantly share code, notes, and snippets.

@kkchu791
Last active November 17, 2015 05:08
Show Gist options
  • Save kkchu791/78862590d345ffabf0f2 to your computer and use it in GitHub Desktop.
Save kkchu791/78862590d345ffabf0f2 to your computer and use it in GitHub Desktop.
class Rectangle
attr_reader :area, :perimeter
def initialize(width, height)
@area = width * height
@perimeter = (width + height)*2
end
end
r = Rectangle.new(23.45, 34.67)
puts "Area is = #{r.area}"
puts "Perimeter is = #{r.perimeter}"
#What if we change the instance variables to be area and perimeter, rather than width and length? What benefits does this give us?
#Shorter code and dimensions of rectangle are not exposed to modification.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment