Another elementary problem

Juergen,

Thanks!

I’m new to Ruby and in general I understand the difference between class scope
and instance scope. But I’m not clear on why the original code wasn’t working.
Can you explain a little more?

Thanks very much again!

···

#######################################
Date: Wed, 27 Nov 2002 17:12:44 +0900
Posted: Wed, 27 Nov 2002 09:12:37 +0100
From: “Lind, Juergen” Juergen.Lind@softwareag.com
Reply-To: ruby-talk@ruby-lang.org
Subject: Re: Another elementary problem… :frowning:

Hi Ted,

say
def S2hhmmss.to_hhmmss(duration)

instead of
def to_hhmmss(duration)

and it should run.

This defines a class method as opposed to an instance method.

Juergen

Christopher J. Meisenzahl CPS, CSTE
Senior Software Testing Consultant
Spherion
christopher.j.meisenzahl@citicorp.com

Hi –

Juergen,

Thanks!

I’m new to Ruby and in general I understand the difference between class scope
and instance scope. But I’m not clear on why the original code wasn’t working.
Can you explain a little more?

In the original, the code was doing something equivalent to this:

class A
def talk
puts “Hello”
end
end

A.talk # error

The problem here is that, while instances of A respond to “talk”, the
class object A itself does not respond to “talk”. That’s another way
of saying: class A does not have a class method called “talk”, only an
instance method.

So, you could rewrite this as:

class A
def A.talk
puts “Hello”
end
end

A.talk # Hello

If that answers your question, you can stop reading :slight_smile: But here’s
some more info about class methods, for your possible reading
pleasure.

Every object responds to certain messages (i.e., can call methods with
certain names). Usually those methods are the instance methods
defined by the object’s class:

class C
def blah
end
end

c = C.new
c.blah

However, it’s also possible to add methods to individual objects:

def c.speak
puts “I’m a method defined only for this object”
end

Now, c will respond to “speak” – but other instances of class C will
not:

c.speak # “I’m a method…”
n = C.new
n.speak # error

This means that “speak” is a singleton method of c.

OK… now look at this:

class D
def D.greet
puts “Hi”
end
end

D.greet # Hi

Notice the similarity between the syntax involved in creating a new
singleton method for c:

def c.speak …

and creating a class method of class D;

def D.greet …

In fact, these are essentially the same thing. In both cases, what’s
happening is that a singleton method is being added to a particular
object. It just happens to be that in the second case, the object
getting the new method is a Class object (as opposed to a String, an
Array, an instance of MyClass, etc). So now D responds to “greet”,
just as c responds to “speak”.

In other words, the term “class method” is just a special term for
something which you can do with any mutable object: namely, add a
singleton method to it. It has a special name because in actual
program design, class methods have a special role to play. But what
they are, at heart, is singleton methods defined on objects, where
those objects happen to be instances of a class called Class.

The use of uppercase names (constants) for classes can obscure the
fact that classes are just objects. Also, the usual style is to put
class method definitions inside the class definition, which makes it
look like they have some special status. But look at this:

class E
end

Defining “class method” outside of class definition:

def E.greet
puts “hi”
end

Dispensing with the constant name for the class:

var = E
def var.talk
puts “This method is defined outside the class definition”
end

var.talk # This method…
E.talk # This method…
var.greet # hi

etc.

You can see that some of the special treatment of classes – constants
as names, the separate notion of “class method” for their singleton
methods – is just that: special treatment. Underneath, a class is
indeed an object.

</unsolicited tutorial>

David

···

On Wed, 27 Nov 2002 christopher.j.meisenzahl@citicorp.com wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

[...]

David

I’ve stored this at http://www.rubygarden.org/ruby?ClassMethodsTutorial.

Gavin

···

From: dblack@candle.superlink.net