Matthias B. wrote:
I am now trying to grasp what a class isand what is is for. I understand
that there a bunch of different classes, but how does that help me?
In your code you use a lot of objects, right? Like 1 is an Integer
“hello” is a String object, /\d+/ is a Regexp object,  is an Array
2…3 is a Range object (made out of two Integer objects) and so on.
You will have noticed that those objects all have a different set of
You can do (2…3).each or [1,3,5].each, but you can’t do 4.each or
You can do “I’m a string”.length, but you can’t do 3.length.
You can do 5.times, but you can’t do “hello”.times.
And it makes sense that it works that way, right? Cause what would the
of an Integer be? Or what would it mean to execute a block of code
As you can see, certain methods only make sense when used with a certain
of object. But how does Ruby know which objects are supposed to have
methods? It know because of classes:
Every object is an instance of a class. “hello” is an instance of class
String, 5 is an instance of class Integer (Fixnum actually, but let’s
care about the difference at this point), 3…4 is an instance of class
and so on. Those classes describe what properties and methods their
have. “hello” has the methods length, gsub, tr and so on because they
defined as methods of String. 2…5 and [“a”,“b”,“c”] have the method
because in the class Range as well as in the class Array there is a
called each. And so on.
But what can you do with classes? Well, you can use those classes by
their instances. But you’re already doing that. Even when you do 3 + 5
using two instances of a class (3 and 5 of class Integer) and one method
that class (the method +).
So what else can you do with classes? For starters you can extend the
classes. Let’s say you have a method that “censors” a string:
You can use it like this: censor(“hello world”), but now you’re thinking
it would be much more fun if you could use it like this: “hello
cause after all: all the other methods working with strings are used
too. So here’s how you’d do that:
Now you can type “hello”.censor and get back “xxxxx” (how very useful,
it). You will have noticed that where we previously said string.gsub we
said self.gsub. Why’s that? That’s because the parameter string doesn’t
anymore. We want to call the method like mystring.censor() without any
paramether. The method should work with the string that’s in front of
(also called the receiver of the method) and not with any parameter. So
do we access the receiver? By using self. Inside a method definition
will point to the object that the method has been called on. So when you
do “hello”.censor, self is “hello” and if you do “bla”.censor, self is
Actually we wouldn’t have even needed to use self in there: If you call
method and you don’t specify a receiver (i.e. you write foo() instead of
self.foo() ), the receiver is assumed to be self. So gsub(…) would be
same as self.gsub(…).
So that’s how you can extend existing classes. But sometimes that’s not
enough. I mean think about what kind of objects does ruby have? Strings,
numbers, ranges of numbers, ranges of strings,… BORING! You’re a
person, you can’t be expected to sit in the basement all day and only
with numbers and strings. So ruby clearly needs a way to use people in
program. So what we do is we create a class called Person that describes
properties a person has and what he/she can do (what methods he/she
that we can then use Person objects (I know you usually shouldn’t think
person as an object, but here it’s okay) in our code. Here’s how to:
First you decide what properties a person should have. For now let’s
small and only let him/her have a name.
def initialize(firstname, surname)
@fname = firstname
@sname = surname
So how do you use this class? Like this:
my_person = Person.new(“John”, “Smith”)
new is a method that can be used on classes. It creates an instance of
class and then calls a method called initialize on it (that’s why we
such a method in our class). So now our initialize method gets called
the parameters firstname and surname. All it does with those two is
them: the value of firstname gets stored in @fname and the value of
in @sname. Variables starting with an @-sign are so called instance
variables. They’re properties of an object. If a method sets an instance
variable to some value, all methods that get called on the same object
now see this value when they access that variable.
Other than initialize there are three other methods: firstname, surname
name. firstname and surname are boring. All they do is return the value
the instance variable @fname or @sname respectively. name is a bit
It uses @fname and @sname to create the full name and then returns that.
course you can do things far more advanced than this with classes, but
should give you something to start with.