F
Fernando Guillen
Hi people.. I was watching the Dave Thomas' talk on the ScotlandOnRails
2009 event about Ruby and the Object.
He say that a class does not exists on the Ruby land, a class is just an
instance of Class class.
So I was wondering, how could I define a 'class' without using the class
sentence .. I mean:
If I do this:
<pre>
class A
def self.class_hello
puts 'hello on class'
end
def instance_hello
puts 'hello on instance'
end
end
</pre>
I have a A class so I can start to instance instances of this class:
<pre>
a = A.new
a.instance_hello
A.class_hello
</pre>
But as Dave says the A class is just constant instance of Class class,
so why not try to define a constant of an instance of Class class and
try to reproduce the same behavior:
<pre>
A = Class.new
def A.class_hello
puts 'hello on class'
end
a = A.new
a.instance_hello # does not work
A.class_hello
</pre>
There it is, I could obtain the behavior of the class_hello but I
couldn't define the instance_hello with this approx.
So my questions are:
1) It is possible to obtain a total behavior of a Class constant without
the 'class' sentence?
2) What is the 'class' sentence?, because it is not a instance_method of
'main' that is an instance of Object, the Object#class method returns
the name of the class of the instance but not receive any kind of
arguments.
This doesn't work:
<pre>
self.class B
def h
puts "h"
end
end
</pre>
This either:
<pre>
Object.class B
def h
puts "h"
end
end
</pre>
So, as you can see I am trying to understand things that they should be
very simple, but for the moment my brain is not ready
f.
2009 event about Ruby and the Object.
He say that a class does not exists on the Ruby land, a class is just an
instance of Class class.
So I was wondering, how could I define a 'class' without using the class
sentence .. I mean:
If I do this:
<pre>
class A
def self.class_hello
puts 'hello on class'
end
def instance_hello
puts 'hello on instance'
end
end
</pre>
I have a A class so I can start to instance instances of this class:
<pre>
a = A.new
a.instance_hello
A.class_hello
</pre>
But as Dave says the A class is just constant instance of Class class,
so why not try to define a constant of an instance of Class class and
try to reproduce the same behavior:
<pre>
A = Class.new
def A.class_hello
puts 'hello on class'
end
a = A.new
a.instance_hello # does not work
A.class_hello
</pre>
There it is, I could obtain the behavior of the class_hello but I
couldn't define the instance_hello with this approx.
So my questions are:
1) It is possible to obtain a total behavior of a Class constant without
the 'class' sentence?
2) What is the 'class' sentence?, because it is not a instance_method of
'main' that is an instance of Object, the Object#class method returns
the name of the class of the instance but not receive any kind of
arguments.
This doesn't work:
<pre>
self.class B
def h
puts "h"
end
end
</pre>
This either:
<pre>
Object.class B
def h
puts "h"
end
end
</pre>
So, as you can see I am trying to understand things that they should be
very simple, but for the moment my brain is not ready
f.