-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f5d623
commit bfa8002
Showing
6 changed files
with
3,266 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Class names must be capitalized. Technically, it's a constant. | ||
class Fred | ||
|
||
# The initialize method is the constructor. The @val is | ||
# an object value. | ||
def initialize(v) | ||
@val = v | ||
end | ||
|
||
# Set it and get it. | ||
def set(v) | ||
@val = v | ||
end | ||
|
||
def get | ||
return @val | ||
end | ||
end | ||
|
||
# Objects are created by the new method of the class object. | ||
a = Fred.new(10) | ||
b = Fred.new(22) | ||
|
||
print "A: ", a.get, " ", b.get,"\n"; | ||
b.set(34) | ||
print "B: ", a.get, " ", b.get,"\n"; | ||
|
||
# Ruby classes are always unfinished works. This does not | ||
# re-define Fred, it adds more stuff to it. | ||
class Fred | ||
def inc | ||
@val += 1 | ||
end | ||
end | ||
|
||
a.inc | ||
b.inc | ||
print "C: ", a.get, " ", b.get,"\n"; | ||
|
||
# Objects may have methods all to themselves. | ||
def b.dec | ||
@val -= 1 | ||
end | ||
|
||
begin | ||
b.dec | ||
a.dec | ||
rescue StandardError => msg | ||
print "Error: ", msg, "\n" | ||
end | ||
|
||
print "D: ", a.get, " ", b.get,"\n"; |
Oops, something went wrong.