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

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

f.

···

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

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

···

On Sat, 27 Jun 2009, Fernando Guillen wrote:

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

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

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.

Yep... simple like this..

Thanks a lot David.

f.

···

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

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

···

On Sat, 27 Jun 2009, Fernando Guillen wrote:

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

A = Class::new( B )

HTH
Robert

···

On 6/26/09, Fernando Guillen <fguillen.mail@gmail.com> wrote:

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

  class A < B
  end

--
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]

Conversations on twitter:




···

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

David A. Black wrote:

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

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.

···

On Sat, 27 Jun 2009, Fernando Guillen wrote:

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

Robert Dober wrote:

···

On 6/26/09, Fernando Guillen <fguillen.mail@gmail.com> 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.
--
Posted via http://www.ruby-forum.com/\.

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

···

On Jun 26, 2009, at 1:22 PM, Fernando Guillen 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

Fernando Guillen wrote:

David A. Black 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

···

On Sat, 27 Jun 2009, Fernando Guillen wrote:

Gary Wright wrote:

···

On Jun 26, 2009, at 1:22 PM, Fernando Guillen 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.
--
Posted via http://www.ruby-forum.com/\.

Jörg W Mittag wrote:

Fernando Guillen wrote:

David A. Black 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.

···

On Sat, 27 Jun 2009, Fernando Guillen wrote:

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

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

http://pastie.org/526389

HTH
Robert

···

On 6/26/09, Fernando Guillen <fguillen.mail@gmail.com> wrote:

Jörg W Mittag wrote:

Fernando Guillen wrote:

David A. Black wrote:

On Sat, 27 Jun 2009, Fernando Guillen 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]

--
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]

Robert Dober 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(c)].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"]

···

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

Hi Fernando,

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

Mat

···

On Sat, Jun 27, 2009 at 11:07, Fernando Guillen<fguillen.mail@gmail.com> wrote:

Robert Dober 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(c)].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"]

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

Robert Dober 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(c)].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?

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

···

On 6/27/09, Fernando Guillen <fguillen.mail@gmail.com> wrote:
--
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]

Mat Brown wrote:

···

On Sat, Jun 27, 2009 at 11:07, Fernando Guillen<fguillen.mail@gmail.com> > 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.
--
Posted via http://www.ruby-forum.com/\.

Robert Dober wrote:

Robert Dober 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:

<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.
</quote>

#["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.

···

On 6/27/09, Fernando Guillen <fguillen.mail@gmail.com> wrote:

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