Trying to define a 'class' without using 'class' sentence

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.
 
D

David A. Black

Hi --

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: [...]
So my questions are:

1) It is possible to obtain a total behavior of a Class constant without
the 'class' sentence?

Yes; you can use a block (among other techniques, like class_eval).

c = Class.new do
def instance_method
puts "hi from instance"
end
def self.class_method
puts "hi from class"
end
end

c.new.instance_method # hi from instance
c.class_method # hi from class
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.

"class" on its own is a keyword, not a method. It's in the same family
as def, if, case, return, next... and a whole bunch of others.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com
 
F

Fernando Guillen

Hi,
Yes; you can use a block (among other techniques, like class_eval).

c = Class.new do
def instance_method
puts "hi from instance"
end
def self.class_method
puts "hi from class"
end
end

c.new.instance_method # hi from instance
c.class_method # hi from class

That looks great, so the Class.initialize method admit a block as a
parameter and the block becomes on the definition of the Class
instance.. (I am trying to find the ruby source-code of Class but I can
not find it on my computer.. :/)

Now, what about inheritance? I mean: how to reproduce the behavior of
this?:

class A < B
end
"class" on its own is a keyword, not a method. It's in the same family
as def, if, case, return, next... and a whole bunch of others.

Yep... simple like this..

Thanks a lot David.

f.
 
D

David A. Black

Hi --

That looks great, so the Class.initialize method admit a block as a
parameter and the block becomes on the definition of the Class
instance.. (I am trying to find the ruby source-code of Class but I can
not find it on my computer.. :/)

If you have the source code installed, look for class.c (and the
closely related module.c).
Now, what about inheritance? I mean: how to reproduce the behavior of
this?:

class A < B
end

Class.new(B) # inherit from B


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com
 
R

Robert Dober

Now, what about inheritance? I mean: how to reproduce the behavior of
this?:

class A < B
end

A =3D Class::new( B )

HTH
Robert
--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]
 
F

Fernando Guillen

David said:
Hi --



If you have the source code installed, look for class.c (and the
closely related module.c).

I though that Class class (also String class, Integer class and so on)
was implemented on ruby code. But with your answer I think this classes
are implemented directly on C.. it is that correct?
Class.new(B) # inherit from B

oook.. and this works too:

C = Class.new(B) do
def instance_method
puts "hi from instance"
end
def self.class_method
puts "hi from class"
end
end

Thanks again

f.
 
G

Gary Wright



Isn't it easier to simply follow a thread on the ruby-talk mailing list?

Threaded email messages (with a public archive) seems much simpler
than code fragments embedded in pastie pages linked from tweets
embedded in twitter archive pages with URLs posted to the ruby-talk
mailing list.

I guess I am just old-fashioned.

Gary Wright
 
J

Jörg W Mittag

Fernando said:
I though that Class class (also String class, Integer class and so on)
was implemented on ruby code.

That depends on what implementation you are using.
But with your answer I think this classes
are implemented directly on C.. it is that correct?

No, it is not correct. Every implementation implements them
differently: XRuby and JRuby implement them in Java, IronRuby and
Ruby.NET implement them in C#, Red Sun in ActionScript, Cardinal in
PIR, MacRuby in Objective-C, MRI, YARV and tinyrb in C.

Only Rubinius and MagLev implement them in Ruby, at least partially.

In Rubinius, a tiny bit of core functionality of modules and classes
is implemented inside the C++ VM, but the majority of functionality is
written in Ruby.

The MagLev sourcode is not publicly available, but AFAIK Ruby classes
are implemented using Smalltalk classes with glue code written in
Smalltalk and compatibility code written in Ruby.

jwm
 
F

Fernando Guillen

Gary said:
Isn't it easier to simply follow a thread on the ruby-talk mailing list?

Threaded email messages (with a public archive) seems much simpler
than code fragments embedded in pastie pages linked from tweets
embedded in twitter archive pages with URLs posted to the ruby-talk
mailing list.

I guess I am just old-fashioned.

Not at all, I think like you, this is because I pasted the tweets here:
because it is here where they suppose to be.

f.
 
F

Fernando Guillen

Jörg W Mittag said:
That depends on what implementation you are using.


No, it is not correct. Every implementation implements them
differently: XRuby and JRuby implement them in Java, IronRuby and
Ruby.NET implement them in C#, Red Sun in ActionScript, Cardinal in
PIR, MacRuby in Objective-C, MRI, YARV and tinyrb in C.

Only Rubinius and MagLev implement them in Ruby, at least partially.

In Rubinius, a tiny bit of core functionality of modules and classes
is implemented inside the C++ VM, but the majority of functionality is
written in Ruby.

The MagLev sourcode is not publicly available, but AFAIK Ruby classes
are implemented using Smalltalk classes with glue code written in
Smalltalk and compatibility code written in Ruby.

Thanks for the explanation.

How can I know with Ruby interpreter I am using?

$ ruby --version
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]

f.
 
R

Robert Dober

J=F6rg W Mittag said:
That depends on what implementation you are using.


No, it is not correct. Every implementation implements them
differently: XRuby and JRuby implement them in Java, IronRuby and
Ruby.NET implement them in C#, Red Sun in ActionScript, Cardinal in
PIR, MacRuby in Objective-C, MRI, YARV and tinyrb in C.

Only Rubinius and MagLev implement them in Ruby, at least partially.

In Rubinius, a tiny bit of core functionality of modules and classes
is implemented inside the C++ VM, but the majority of functionality is
written in Ruby.

The MagLev sourcode is not publicly available, but AFAIK Ruby classes
are implemented using Smalltalk classes with glue code written in
Smalltalk and compatibility code written in Ruby.

Thanks for the explanation.

How can I know with Ruby interpreter I am using?

$ ruby --version
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
and from within Ruby there are several pieces of information you might
find useful:

http://pastie.org/526389

HTH
Robert
--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]
 
F

Fernando Guillen

Robert said:
and from within Ruby there are several pieces of information you might
find useful:

http://pastie.org/526389

Thanks Robert.. very funny code :)

But still I don't know too much about my interpreter:

$ ruby -e 'puts Object.constants.grep( /RUBY/ ).map{ |c| "##{[c,
Object.const_get(c)].inspect}"}'
#["RUBY_VERSION", "1.8.6"]
#["RUBY_PATCHLEVEL", 287]
#["RUBY_RELEASE_DATE", "2008-08-11"]
#["RUBY_PLATFORM", "universal-darwin9.0"]

:/

Is this the relevant information about the kind of my interpreter is?

#["RUBY_PLATFORM", "universal-darwin9.0"]
 
M

Mat Brown

Robert said:
and from within Ruby there are several pieces of information you might
find useful:

http://pastie.org/526389

Thanks Robert.. very funny code :)

But still I don't know too much about my interpreter:

=C2=A0$ ruby -e 'puts Object.constants.grep( /RUBY/ ).map{ |c| "##{[c,
Object.const_get(c)].inspect}"}'
=C2=A0#["RUBY_VERSION", "1.8.6"]
=C2=A0#["RUBY_PATCHLEVEL", 287]
=C2=A0#["RUBY_RELEASE_DATE", "2008-08-11"]
=C2=A0#["RUBY_PLATFORM", "universal-darwin9.0"]

:/

Is this the relevant information about the kind of my interpreter is?

=C2=A0#["RUBY_PLATFORM", "universal-darwin9.0"]

Hi Fernando,

You're running the standard 1.8 Ruby interpreter, otherwise known as MRI.

Mat
 
R

Robert Dober

Thanks Robert.. very funny code :)
But still I don't know too much about my interpreter:

$ ruby -e 'puts Object.constants.grep( /RUBY/ ).map{ |c| "##{[c,
Object.const_get(c)].inspect}"}'
#["RUBY_VERSION", "1.8.6"]
#["RUBY_PATCHLEVEL", 287]
#["RUBY_RELEASE_DATE", "2008-08-11"]
#["RUBY_PLATFORM", "universal-darwin9.0"]

:/

Is this the relevant information about the kind of my interpreter is?
As far as I am concerned this contains about anything you need to
know. Are you missing something specific?
#["RUBY_PLATFORM", "universal-darwin9.0"]
This is the platform Ruby was built for, seems to be OS-X.
But the VERSION and PATCHLEVEL is more important concerning the
"behavior" of the interpreter or VM. In your case MRI with a quite
recent patchlevel. Congrats ;)
This, however is a more conservative Ruby version. You might want
1.8.7 or 1.9.1 for exploration.

Remark that JRuby uses "java" as a platform. This is surely to
indicate that it will run wherever Java runs.
HTH
Robert
--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]
 
F

Fernando Guillen

Robert said:
As far as I am concerned this contains about anything you need to
know. Are you missing something specific?

I was expecting something more explicit, some one of the list Jörg
offered us:

<quote>
XRuby and JRuby implement them in Java, IronRuby and
Ruby.NET implement them in C#, Red Sun in ActionScript, Cardinal in
PIR, MacRuby in Objective-C, MRI, YARV and tinyrb in C.
#["RUBY_PLATFORM", "universal-darwin9.0"]
This is the platform Ruby was built for, seems to be OS-X.
But the VERSION and PATCHLEVEL is more important concerning the
"behavior" of the interpreter or VM. In your case MRI with a quite
recent patchlevel. Congrats ;)
This, however is a more conservative Ruby version. You might want
1.8.7 or 1.9.1 for exploration.

Thanks for the explanation Robert

f.
 

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
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top