Aww… My very first Ruby class! This is preserved exactly as originally written, and you’ll notice I didn’t fully have a grasp on the Ruby conventions yet :)
Just bear in mind that camelCase isn’t very Ruby ;)
Download this source
class FirstClass
PREFIX = "Arg is "
def firstMethod(a)
print PREFIX
print a
print "\n"
end
end
c = FirstClass.new
c.firstMethod("Rosco")
c.firstMethod(77)
c.firstMethod(c)
# Heres a slightly more complex example
class SecondClass
# a constant
ACONST = "Constant"
# constructor
def initialize(name, location)
@name = name;
@location = location
end
def sayHello
puts "Hello there, #{@name}! So you're from #{@location} huh?"
end
end
sc = SecondClass.new("Ross Bamford","England")
sc.sayHello
sc = SecondClass.new("Rosie Randall","England")
sc.sayHello
# Bear in mind we can always add stuff to a class. Ruby has the 'to_s' method
# which is much like toString in Java. So let's override that now
puts sc.to_s
class SecondClass
def to_s
"Person: #{@name} from #{@location}"
end
end
puts sc.to_s
Running this outputs:
Arg is Rosco
Arg is 77
Arg is #<FirstClass:0xb7f55f1c>
Hello there, Ross Bamford! So you're from England huh?
Hello there, Rosie Randall! So you're from England huh?
#<SecondClass:0xb7f55d14>
Person: Rosie Randall from England

