Forum: Ruby class <=> module

Cd3fcb7a417c8acb989fc320b604a2a8?d=identicon&s=25 Bráulio Bhavamitra (brauliobo)
on 2013-07-24 01:04
Hello all,

Recently I got the error described at
https://www.ruby-forum.com/topic/1506818,
which shows that classes can't be used to scope another classes or
modules.

Then I started thinking: why two concepts, class and module, instead of
only one?

Classes heritage, a feature modules don't have, can be done with include
and extend togheter. So we would need another method that does both
include and extend (call it inherit). Classes can't be used to define a
scope.
Modules can't be instanciated, but classes can.

So why not have only one concept that does both class and modules? You
may call this unified concept either class or a module, I'll call it X.

X is capable of extending, including and inheriting, does scoping and be
instanciated.

How does it sound?

regards,
bráulio
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2013-07-24 02:55
(Received via mailing list)
On Jul 23, 2013, at 16:04 , Brulio Bhavamitra <lists@ruby-forum.com>
wrote:

> Recently I got the error described at
> https://www.ruby-forum.com/topic/1506818,
> which shows that classes can't be used to scope another classes or
> modules.

that's simply not true.
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2013-07-24 08:10
(Received via mailing list)
Am 24.07.2013 01:04, schrieb Bráulio Bhavamitra:
> Classes heritage, a feature modules don't have, can be done with include
>
> How does it sound?

That sounds like: We have two different things that behave differently
and serve different purposes. Why don't we replace them by one thing.

Not very convincing to me... :)

And you forgot to mention: classes can inherit only from a single other
class (a conscious decision by Matz), but you can include many modules.
Seems difficult to unify that in one concept.

Regards,
Marcus
B31e7abd14f1ceb4c4957da08933c630?d=identicon&s=25 Josh Cheek (josh-cheek)
on 2013-07-24 14:36
(Received via mailing list)
On Wed, Jul 24, 2013 at 1:10 AM, <sto.mar@web.de> wrote:

>
> And you forgot to mention: classes can inherit only from a single other
> class (a conscious decision by Matz), but you can include many modules.
> Seems difficult to unify that in one concept.
>
>
That seems inaccurate to me:

C1 = Class.new Object
C2 = Class.new C1
C3 = Class.new C2

M1 = Module.new
M2 = Module.new
C4 = Class.new { include M2, M1 }
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2013-07-24 14:52
(Received via mailing list)
Am 24.07.2013 14:35, schrieb Josh Cheek:
>
> C1 = Class.new Object
> C2 = Class.new C1
> C3 = Class.new C2

I was alluding to multiple inheritance, meaning a class
inherits _directly_ from more than one superclass.

> M1 = Module.new
> M2 = Module.new
> C4 = Class.new { include M2, M1 }

Regards,
Marcus
Cd3fcb7a417c8acb989fc320b604a2a8?d=identicon&s=25 Bráulio Bhavamitra (brauliobo)
on 2013-07-24 14:58
Ryan Davis wrote in post #1116443:
> On Jul 23, 2013, at 16:04 , Brulio Bhavamitra <lists@ruby-forum.com>
> wrote:
>
>> Recently I got the error described at
>> https://www.ruby-forum.com/topic/1506818,
>> which shows that classes can't be used to scope another classes or
>> modules.
>
> that's simply not true.

So why in rails activerecord is defined as ActiveRecord::Base instead of
just ActiveRecord?
Cd3fcb7a417c8acb989fc320b604a2a8?d=identicon&s=25 Bráulio Bhavamitra (brauliobo)
on 2013-07-24 14:59
unknown wrote in post #1116513:
> Am 24.07.2013 14:35, schrieb Josh Cheek:
>>
>> C1 = Class.new Object
>> C2 = Class.new C1
>> C3 = Class.new C2
>
> I was alluding to multiple inheritance, meaning a class
> inherits _directly_ from more than one superclass.
The point is that inheritance may be done with include + extend from a
module.
In fact, many libraries already prefer include/extend from module than
heritage. See MongoMapper for example.

>
>> M1 = Module.new
>> M2 = Module.new
>> C4 = Class.new { include M2, M1 }
>
> Regards,
> Marcus
Cd3fcb7a417c8acb989fc320b604a2a8?d=identicon&s=25 Bráulio Bhavamitra (brauliobo)
on 2013-07-24 15:05
> That sounds like: We have two different things that behave differently
> and serve different purposes. Why don't we replace them by one thing.
>
> Not very convincing to me... :)
The thing is that sometimes both features, from classes and modules, are
needed in just one thing.
Cd3fcb7a417c8acb989fc320b604a2a8?d=identicon&s=25 Bráulio Bhavamitra (brauliobo)
on 2013-07-24 15:23
> X is capable of extending, including and inheriting, does scoping and be
> instanciated.
>

I'll give some extra explanation with an example.

With today's model of inheritance, sometimes you get stuck with the
parent class and is forced to move methods to a module and
include/extend them.

So instead of inheritance in class definition (with the <), I propose a
method inherit, which does both include and extend.

x A
  inherit D
  extend C

  # ... class and instance methods ...
end
x B
  include C

  # ... class and instance methods ...
end
x C

  # ... class and instance methods ...
end
x D

  # ... class and instance methods ...
end
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2013-07-24 15:28
(Received via mailing list)
Am 24.07.2013 14:59, schrieb Bráulio Bhavamitra:
> module.
But that is not the same as subclassing.

> In fact, many libraries already prefer include/extend from module than
> heritage. See MongoMapper for example.

Both approaches have their own use cases.

Regards,
Marcus
B31e7abd14f1ceb4c4957da08933c630?d=identicon&s=25 Josh Cheek (josh-cheek)
on 2013-07-24 15:35
(Received via mailing list)
On Wed, Jul 24, 2013 at 7:51 AM, <sto.mar@web.de> wrote:

>>     Seems difficult to unify that in one concept.
> inherits _directly_ from more than one superclass.
>
>
I still don't understand. I showed that you can inherit from multiple
classes like you can inherit from multiple modules. Yes there are other
differences, but this does not seem to be one of them. Perhaps you think
that modules have some sort of fancier inheritance? If so, this is not
the
case. When you include a module, an anonymous class with the module's
methods gets inserted as the superclass.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-07-24 17:25
(Received via mailing list)
On Wed, Jul 24, 2013 at 3:34 PM, Josh Cheek <josh.cheek@gmail.com>
wrote:
>>>     class (a conscious decision by Matz), but you can include many
>>
>> I was alluding to multiple inheritance, meaning a class
>> inherits _directly_ from more than one superclass.
>>
>
> I still don't understand. I showed that you can inherit from multiple
> classes like you can inherit from multiple modules. Yes there are other
> differences, but this does not seem to be one of them. Perhaps you think
> that modules have some sort of fancier inheritance? If so, this is not the
> case. When you include a module, an anonymous class with the module's
> methods gets inserted as the superclass.

You cannot extend from arbitrary classes. If I want to create a class
that extends String and Hash, for example, I can't.
On the other hand I can extend any number of arbitrary modules.

Jesus.
B31e7abd14f1ceb4c4957da08933c630?d=identicon&s=25 Josh Cheek (josh-cheek)
on 2013-07-24 18:03
(Received via mailing list)
On Wed, Jul 24, 2013 at 10:25 AM, Jess Gabriel y Galn <
jgabrielygalan@gmail.com> wrote:

> >>> other
> >>
> > case. When you include a module, an anonymous class with the module's
> > methods gets inserted as the superclass.
>
> You cannot extend from arbitrary classes. If I want to create a class
> that extends String and Hash, for example, I can't.
> On the other hand I can extend any number of arbitrary modules.
>
> Jesus.
>
>
I'm not sure that's what he was trying to say.
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2013-07-24 19:55
(Received via mailing list)
Am 24.07.2013 15:34, schrieb Josh Cheek:
>             And you forgot to mention: classes can inherit only from a
>         C3 = Class.new C2
>
>
>     I was alluding to multiple inheritance, meaning a class
>     inherits _directly_ from more than one superclass.
>
>
> I still don't understand. I showed that you can inherit from multiple
> classes like you can inherit from multiple modules. Yes there are other

No, they behave differently!
The modules are *not* superclasses of the classes they are mixed into:

  C1 = Class.new Object
  C2 = Class.new C1
  C3 = Class.new C2

  C3.superclass  # => C2
  C3.superclass.superclass  # => C1

  M1 = Module.new
  M2 = Module.new
  C4 = Class.new { include M2, M1 }

  C4.superclass  # => Object
B31e7abd14f1ceb4c4957da08933c630?d=identicon&s=25 Josh Cheek (josh-cheek)
on 2013-07-24 20:23
(Received via mailing list)
On Wed, Jul 24, 2013 at 12:54 PM, <sto.mar@web.de> wrote:

> >
> >         C2 = Class.new C1
> No, they behave differently!
> The modules are *not* superclasses of the classes they are mixed into:
>
>
They are, the code examples just say otherwise because Ruby lies to you.
It's the same as how the singleton class is actually the object's class,
even though when you ask it about its class, it will tell you what it
was
instantiated from.

This used to be a good blog about it
http://web.archive.org/web/20110214135710/http://c...
sadly it's down now, so you can't see the images.

-Josh
Cd3fcb7a417c8acb989fc320b604a2a8?d=identicon&s=25 Bráulio Bhavamitra (brauliobo)
on 2013-07-25 08:01
(Received via mailing list)
On Wed, Jul 24, 2013 at 9:48 AM, Bráulio Bhavamitra
<braulio@eita.org.br>wrote:

>> > https://www.ruby-forum.com/topic/1506818,
>
>
>
> --
> "Lute pela sua ideologia. Seja um com sua ideologia. Viva pela sua
> ideologia. Morra por sua ideologia" P.R. Sarkar
>
> EITA - Educação, Informação e Tecnologias para Autogestão
> http://cirandas.net/brauliobo
> http://eita.org.br
>



--
"Lute pela sua ideologia. Seja um com sua ideologia. Viva pela sua
ideologia. Morra por sua ideologia" P.R. Sarkar

EITA - Educação, Informação e Tecnologias para Autogestão
http://cirandas.net/brauliobo
http://eita.org.br
Cd3fcb7a417c8acb989fc320b604a2a8?d=identicon&s=25 Bráulio Bhavamitra (brauliobo)
on 2013-07-25 08:02
(Received via mailing list)
On Wed, Jul 24, 2013 at 9:54 AM, Bráulio Bhavamitra
<braulio@eita.org.br>wrote:

>>>
>>> C2 = Class.new C1
>>
>> <https://github.com/stomar/>
> http://eita.org.br
>



--
"Lute pela sua ideologia. Seja um com sua ideologia. Viva pela sua
ideologia. Morra por sua ideologia" P.R. Sarkar

EITA - Educação, Informação e Tecnologias para Autogestão
http://cirandas.net/brauliobo
http://eita.org.br
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.