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


#1

Hi people… I was watching the Dave T.’ 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:

 class A
   def self.class_hello
     puts 'hello on class'
   end

   def instance_hello
     puts 'hello on instance'
   end
 end
 

I have a A class so I can start to instance instances of this class:

 a = A.new
 a.instance_hello
 A.class_hello
 

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:

 A = Class.new
 def A.class_hello
   puts 'hello on class'
 end

 a = A.new
 a.instance_hello # does not work
 A.class_hello
 

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:

 self.class B
   def h
     puts "h"
   end
 end
 

This either:

 Object.class B
   def h
     puts "h"
   end
 end
 

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 :slight_smile:

f.


#2

Hi –

On Sat, 27 Jun 2009, Fernando G. wrote:

Hi people… I was watching the Dave T.’ 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

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


#3

Hi,

David A. Black wrote:

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

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

Yep… simple like this…

Thanks a lot David.

f.


#4

Hi –

On Sat, 27 Jun 2009, Fernando G. wrote:

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


#5

On 6/26/09, Fernando G. removed_email_address@domain.invalid wrote:

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

class A < B
end

A = Class::new( B )

HTH
Robert

Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

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

[Antoine de Saint-Exupéry]


#6

Robert D. wrote:

On 6/26/09, Fernando G. removed_email_address@domain.invalid wrote:

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

class A < B
end

A = Class::new( B )

Thanks Robert

f.


#7

David A. Black wrote:

Hi –

On Sat, 27 Jun 2009, Fernando G. wrote:

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

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?

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

class A < B
end

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.


#8

Conversations on twitter:

http://twitter.com/isaac_feliu/status/2345860826
http://twitter.com/porras/status/2345958703
http://twitter.com/fxn/status/2346151294
http://twitter.com/fxn/status/2346180342


#9

On Jun 26, 2009, at 1:22 PM, Fernando G. wrote:

Conversations on twitter:

http://twitter.com/isaac_feliu/status/2345860826
http://twitter.com/porras/status/2345958703
http://twitter.com/fxn/status/2346151294
http://twitter.com/fxn/status/2346180342

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


#10

Fernando G. wrote:

David A. Black wrote:

On Sat, 27 Jun 2009, Fernando G. wrote:

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


#11

Gary W. wrote:

On Jun 26, 2009, at 1:22 PM, Fernando G. wrote:

Conversations on twitter:

http://twitter.com/isaac_feliu/status/2345860826
http://twitter.com/porras/status/2345958703
http://twitter.com/fxn/status/2346151294
http://twitter.com/fxn/status/2346180342

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.


#12

Jörg W Mittag wrote:

Fernando G. wrote:

David A. Black wrote:

On Sat, 27 Jun 2009, Fernando G. wrote:

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

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.


#13

On 6/26/09, Fernando G. removed_email_address@domain.invalid wrote:

I though that Class class (also String class, Integer class and so on)
PIR, MacRuby in Objective-C, MRI, YARV and tinyrb in C.

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

Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

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

[Antoine de Saint-Exupéry]


#14

Robert D. wrote:

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

http://pastie.org/526389

Thanks Robert… very funny code :slight_smile:

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

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

:confused:

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

#[“RUBY_PLATFORM”, “universal-darwin9.0”]


#15

Mat B. wrote:

On Sat, Jun 27, 2009 at 11:07, Fernando G.removed_email_address@domain.invalid
wrote:

 #[“RUBY_PLATFORM”, “universal-darwin9.0”]


Posted via http://www.ruby-forum.com/.

Hi Fernando,

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

Thanks Mat

f.


#16

On Sat, Jun 27, 2009 at 11:07, Fernando G.removed_email_address@domain.invalid
wrote:

 #[“RUBY_PLATFORM”, “universal-darwin9.0”]


Posted via http://www.ruby-forum.com/.

Hi Fernando,

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

Mat


#17

On 6/27/09, Fernando G. removed_email_address@domain.invalid wrote:

Robert D. wrote:

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

http://pastie.org/526389

Thanks Robert… very funny code :slight_smile:

:confused:

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 :wink:
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

Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

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

[Antoine de Saint-Exupéry]


#18

Robert D. wrote:

On 6/27/09, Fernando G. removed_email_address@domain.invalid wrote:

Robert D. wrote:

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

http://pastie.org/526389

Thanks Robert… very funny code :slight_smile:

:confused:

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?

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

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 :wink:
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.