Forum: Ruby Trying to define a 'class' without using 'class' sentence

6f952bee7570a3db2ecba5b06c0062b3?d=identicon&s=25 Fernando Guillen (fguillen)
on 2009-06-26 17:09
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.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-06-26 17:23
(Received via mailing list)
Hi --

On Sat, 27 Jun 2009, Fernando Guillen wrote:

> 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
6f952bee7570a3db2ecba5b06c0062b3?d=identicon&s=25 Fernando Guillen (fguillen)
on 2009-06-26 17:35
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.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-06-26 18:10
(Received via mailing list)
Hi --

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

> Now, what about inheritance? I mean: how to reproduce the behavior of
> this?:
>
>  class A < B
>  end

Class.new(B)  # inherit from B


David
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-06-26 18:14
(Received via mailing list)
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 )

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]
6f952bee7570a3db2ecba5b06c0062b3?d=identicon&s=25 Fernando Guillen (fguillen)
on 2009-06-26 19:12
David A. Black wrote:
> Hi --
>
> 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. 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.
6f952bee7570a3db2ecba5b06c0062b3?d=identicon&s=25 Fernando Guillen (fguillen)
on 2009-06-26 19:12
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.
6f952bee7570a3db2ecba5b06c0062b3?d=identicon&s=25 Fernando Guillen (fguillen)
on 2009-06-26 19:21
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2009-06-26 20:41
(Received via mailing list)
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.

Gary Wright
Ba91caa4b1fd66471e4180151b379dd7?d=identicon&s=25 Jörg W Mittag (Guest)
on 2009-06-26 21:00
(Received via mailing list)
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.

jwm
6f952bee7570a3db2ecba5b06c0062b3?d=identicon&s=25 Fernando Guillen (fguillen)
on 2009-06-26 22:45
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.
6f952bee7570a3db2ecba5b06c0062b3?d=identicon&s=25 Fernando Guillen (fguillen)
on 2009-06-26 22:47
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]

f.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-06-27 16:16
(Received via mailing list)
On 6/26/09, Fernando Guillen <fguillen.mail@gmail.com> 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]
6f952bee7570a3db2ecba5b06c0062b3?d=identicon&s=25 Fernando Guillen (fguillen)
on 2009-06-27 17:07
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 :)

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"]
968e5e19e3ce6ccfbb26c311ed872f5d?d=identicon&s=25 Mat Brown (Guest)
on 2009-06-27 18:11
(Received via mailing list)
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.

Mat
6f952bee7570a3db2ecba5b06c0062b3?d=identicon&s=25 Fernando Guillen (fguillen)
on 2009-06-27 18:44
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.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-06-27 18:54
(Received via mailing list)
On 6/27/09, 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 :)

> :/
>
> 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
--
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]
6f952bee7570a3db2ecba5b06c0062b3?d=identicon&s=25 Fernando Guillen (fguillen)
on 2009-06-27 19:42
Robert Dober wrote:
> On 6/27/09, 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 :)
>
>> :/
>>
>> 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 ;)
> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.