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

**URL:** <https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120>\
**Category:** Ruby\
**Created:** [June 26, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120 "2009-06-26T17:09:34Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/1 "2009-06-26T17:09:34Z")

</div>

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 🙂

f.

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/2 "2009-06-26T17:23:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/3 "2009-06-26T17:35:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/4 "2009-06-26T18:10:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/5 "2009-06-26T18:14:06Z")

</div>

On 6/26/09, Fernando G. [removed\_email\_address@domain.invalid](mailto: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]

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 7:12pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/6 "2009-06-26T19:12:27Z")

</div>

Robert D. wrote:

> On 6/26/09, Fernando G. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 7:12pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/7 "2009-06-26T19:12:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/8 "2009-06-26T19:21:58Z")

</div>

Conversations on twitter:

[http://twitter.com/isaac\_feliu/status/2345860826](http://twitter.com/isaac_feliu/status/2345860826)  
[http://twitter.com/porras/status/2345958703](http://twitter.com/porras/status/2345958703)  
[http://twitter.com/fxn/status/2346151294](http://twitter.com/fxn/status/2346151294)  
[http://twitter.com/fxn/status/2346180342](http://twitter.com/fxn/status/2346180342)

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/9 "2009-06-26T20:41:43Z")

</div>

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

> Conversations on twitter:
> 
> [http://twitter.com/isaac\_feliu/status/2345860826](http://twitter.com/isaac_feliu/status/2345860826)  
> [http://twitter.com/porras/status/2345958703](http://twitter.com/porras/status/2345958703)  
> [http://twitter.com/fxn/status/2346151294](http://twitter.com/fxn/status/2346151294)  
> [http://twitter.com/fxn/status/2346180342](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.

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 9:00pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/10 "2009-06-26T21:00:30Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/11 "2009-06-26T22:45:23Z")

</div>

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/isaac_feliu/status/2345860826)  
> > [http://twitter.com/porras/status/2345958703](http://twitter.com/porras/status/2345958703)  
> > [http://twitter.com/fxn/status/2346151294](http://twitter.com/fxn/status/2346151294)  
> > [http://twitter.com/fxn/status/2346180342](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.

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 26, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/12 "2009-06-26T22:47:48Z")

</div>

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](http://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.

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 27, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/13 "2009-06-27T16:16:37Z")

</div>

On 6/26/09, Fernando G. [removed\_email\_address@domain.invalid](mailto: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](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]

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 27, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/14 "2009-06-27T17:07:14Z")

</div>

Robert D. wrote:

> and from within Ruby there are several pieces of information you might  
> find useful:
> 
> [http://pastie.org/526389](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”]

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 27, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/15 "2009-06-27T18:44:52Z")

</div>

Mat B. wrote:

> On Sat, Jun 27, 2009 at 11:07, Fernando G.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Â #[“RUBY\_PLATFORM”, “universal-darwin9.0”]
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Hi Fernando,
> 
> You’re running the standard 1.8 Ruby interpreter, otherwise known as  
> MRI.

Thanks Mat

f.

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 27, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/16 "2009-06-27T18:11:58Z")

</div>

On Sat, Jun 27, 2009 at 11:07, Fernando G.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Â #[“RUBY\_PLATFORM”, “universal-darwin9.0”]
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hi Fernando,

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

Mat

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 27, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/17 "2009-06-27T18:54:57Z")

</div>

On 6/27/09, Fernando G. [removed\_email\_address@domain.invalid](mailto: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](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]

---

<div class="post-metadata">

**Author:** ![fearx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fearx](https://www.ruby-forum.com/u/fearx)\
**Post date:** [June 27, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/trying-to-define-a-class-without-using-class-sentence/171120/18 "2009-06-27T19:42:09Z")

</div>

Robert D. wrote:

> On 6/27/09, Fernando G. [removed\_email\_address@domain.invalid](mailto: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](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:

 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.
