Subclassing self

J

John

X-No-archive: yes

I'm a new ruby user. I have the Ruby Way book.

My question is, in what case would you use

class TheClass
class << self
def foo

Couldn't you just say

class TheClass
def foo

?
 
F

Fredrik Jagenheim

Instead of using the class << self notation, you could do this:

class TheClass
def TheClass::foo

But that can get tedious when defining multiple class methods,
plus you run the risk of missing a change if you rename the class
later.

I was just shown:

class TheClass
def self.foo

This way you don't risk the missing a change when you rename the
class. You still get the tedious typing though. :)

However, what does 'class << self' really mean? Why is this one
of the 'obvious' ways to define class methods?

No, I'm not being sarcastic, just realizing that I don't know the ruby
language as well as I would want to. To me it looks like we're
shifting the current class into an anonymous class, and how that can
spell 'class methods' I really can't see. :)

//F
 
M

Mauricio Fernández

I was just shown:

class TheClass
def self.foo

This way you don't risk the missing a change when you rename the
class. You still get the tedious typing though. :)

However, what does 'class << self' really mean? Why is this one
of the 'obvious' ways to define class methods?

class << obj opens the singleton class of obj. In a class context,
self points to the Class object, therefore in

class A
self # this is A
class << self # same as class << A but needs not be changed if
# if A is renamed
# we're in A's singleton class
end
end

Class methods are in fact class singleton methods (ie. singleton methods
of the object of class Class). You sometimes want to use the class << self
idiom to create attribute accessors for the class, etc:

class A
class << self
attr_accessor :foo
end
end

A.foo = 1
A.foo # => 1

--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Make it idiot-proof, and someone will breed a better idiot.
-- Oliver Elphick
 
G

Gavin Sinclair

I was just shown:
class TheClass
def self.foo
This way you don't risk the missing a change when you rename the
class. You still get the tedious typing though. :)
However, what does 'class << self' really mean? Why is this one
of the 'obvious' ways to define class methods?

It's not necessarily obvious; it's an idiom, but it makes perfect
sense once you know, and it involves core Ruby concepts, so it is
worth knowing.

s = "Hi"
class << s
def foo # Singleton method on object s
5
end
end
s.foo # -> 5

s = String
class << s
def foo # Singleton method on object String
5
end
end
s.foo # -> 5
String.foo # -> 5

See http://www.rubygarden.org/ruby?ClassMethods for more info.

Gavin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,301
Latest member
SusannaCgx

Latest Threads

Top