Skip to content

Instantly share code, notes, and snippets.

@Zilenc3
Created June 3, 2015 04:10
Show Gist options
  • Save Zilenc3/1beb79c01495722b09af to your computer and use it in GitHub Desktop.
Save Zilenc3/1beb79c01495722b09af to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -w
# define a class
class Box
# constructor method
def initialize(w,h)
@width, @height = w, h
end
# instance method by default it is public
def getArea
getWidth() * getHeight
end
# define private accessor methods
def getWidth
@width
end
def getHeight
@height
end
# make them private
private :getWidth, :getHeight
# instance method to print area
def printArea
@area = getWidth() * getHeight
puts "Big box area is : #@area"
end
# make it protected
protected :printArea
end
# create an object
box = Box.new(10, 20)
# call instance methods
a = box.getArea()
puts "Area of the box is : #{a}"
# try to call protected or methods
box.printArea()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment