2 classes, same name

did you know Ruby can have mutiple classes defined with the exact same
constant, yet they be different classes?

module X1
end

module X2
end

class XX
def XX.record(*mods)
x = XX.clone
x.class_eval { include *mods }
return x
end
def initialize
puts self.class.included_modules
puts self.class
puts
end
end

xx = XX.record(X1, X2).new
yy = XX.record(X1).new

···


tom sawyer, aka transami
transami@transami.net

did you know Ruby can have mutiple classes defined with the exact same
constant, yet they be different classes?

module X1
end

module X2
end

class XX
def XX.record(*mods)
x = XX.clone
x.class_eval { include *mods }
return x
end
def initialize
puts self.class.included_modules
puts self.class
puts self.class.id
puts
end
end

xx = XX.record(X1, X2).new
yy = XX.record(X1).new
puts “XX: #{XX.id}”

Errr, no?

batsman@tux-chan:/tmp$ ruby e.rb
X2
X1
Kernel
XX
538072192

X1
Kernel
XX
538071272

XX: 538072512

···

On Sat, Feb 01, 2003 at 09:39:26PM +0900, Tom Sawyer wrote:


_ _

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

damn my office is cold.
need a hot secretary to warm it up.
– Seen on #Linux

yes there are three different classes. just that they all have identical
reponses from #class, which i thought was interesting.

···

On Saturday 01 February 2003 06:12 am, Mauricio Fernández wrote:

Errr, no?

batsman@tux-chan:/tmp$ ruby e.rb
X2
X1
Kernel
XX
538072192

X1
Kernel
XX
538071272

XX: 538072512


tom sawyer, aka transami
transami@transami.net

It’s not an identical response, but a Class value that is printed the same
way :slight_smile: It happens all the time in irb. Perhaps we should fix Class#inspect
and/or Class.to_s .

batsman@tux-chan:/tmp$ cat e.rb
module X1
end

module X2
end

class XX
def XX.record(*mods)
x = XX.clone
x.class_eval { include *mods }
return x
end
def initialize
puts self.class.included_modules
puts self.class
puts self.class.id
puts
end

def XX.inspect
“<Class XX: #{self.id.to_s}>”
end

def XX.to_s
“<Class XX: #{self.id.to_s}>”
end
end

xx = XX.record(X1, X2).new
yy = XX.record(X1).new

puts “XX: #{XX.id}”
batsman@tux-chan:/tmp$ ruby e.rb
X2
X1
Kernel
<Class XX: 538072072>
538072072

X1
Kernel
<Class XX: 538070822>
538070822

XX: 538072512

···

On Sat, Feb 01, 2003 at 11:06:00PM +0900, Tom Sawyer wrote:

On Saturday 01 February 2003 06:12 am, Mauricio Fernández wrote:

Errr, no?

batsman@tux-chan:/tmp$ ruby e.rb
X2
X1
Kernel
XX
538072192

X1
Kernel
XX
538071272

XX: 538072512

yes there are three different classes. just that they all have identical
reponses from #class, which i thought was interesting.


_ _

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

Software is like sex; it’s better when it’s free.
– Linus Torvalds