Created
June 3, 2015 04:10
-
-
Save Zilenc3/1beb79c01495722b09af to your computer and use it in GitHub Desktop.
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
#!/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