# Class names must be capitalized. Technically, it's a constant.
# The initialize method is the constructor. The @val is
# an object value.
@val = v
# Set it and get it.
@val = v
return "Fred(val=" + @val.to_s + ")"
# Since a simple access function is so common, ruby lets you declare one
# automatically, like this:
# You can list any number of object variables. Separate by commas, and each
# needs its own colon
# attr_reader :fred, :joe, :alex, :sally
class Alice <Fred
# We have a message, too.
def initialize(n, m)
@msg = m
# Takes the base result and changes the class name.
ret = super
return ret + ' ' + @msg + '!'
# The = allows the method to be used on the right, and the left of the
# assignment is the parameter.
@msg += more
# Like attr_reader, if you want the data to be assignable.
a = Fred.new(45)
b = Alice.new(11, "So there")
print "A: a = ", a, "\n b = ", b, "\n"
print "B: ", a.val, " ", b.val, "\n"
b.msg = "Never"
print "B: b = ", b, "\n"
b.appmsg = " In a million years"
print "C: b = ", b, "\n"