Skip to content

Commit

Permalink
Add ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
georgewfraser committed May 25, 2019
1 parent 8f5d623 commit bfa8002
Show file tree
Hide file tree
Showing 6 changed files with 3,266 additions and 1 deletion.
52 changes: 52 additions & 0 deletions examples/ruby/classes.rb
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";
Loading

0 comments on commit bfa8002

Please sign in to comment.