From: Eric MSP Veith on
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello List,

what's the difference between:

- ---%<---
namespace A
class B
def foo
# ...
end
end
end
- --->%---

and

- ---%<---
class C
def bar
# ...
end
end
- --->%---

Besindes, of course, the naming (A::B vs C)?

TIA!

Eric
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkweVUQACgkQhS0drJ3goJK3gwCeLhJ9LxyLgnVZ8NNdBFr8XyuI
7tQAnRmAqJe2Y9lOWL5ybMLIPKeFaw8l
=dzBP
-----END PGP SIGNATURE-----

From: Brian Candler on
Eric MSP Veith wrote:
> what's the difference between:
>
> - ---%<---
> namespace A

you mean 'module A' ?

> class B
> def foo
> # ...
> end
> end
> end
> - --->%---
>
> and
>
> - ---%<---
> class C
> def bar
> # ...
> end
> end
> - --->%---
>
> Besindes, of course, the naming (A::B vs C)?

That's the main difference. Constant lookups are modified, e.g.
referring to 'D' inside B will look for A::B::D, A::D or ::D in that
order (I believe)

This can lead to some confusing errors, e.g.

module A
class B
def bar
Socket.new('127.0.0.1',80)
end
end
end
A::B.new.bar # uninitialized constant A::B::Socket (NameError)

when actually the problem is that you forgot to "require 'socket'"
--
Posted via http://www.ruby-forum.com/.

From: Eric MSP Veith on
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Brian,

On Monday 21 June 2010, Brian Candler <b.candler(a)pobox.com> wrote:
> > - ---%<---
> > namespace A
>
> you mean 'module A' ?

Err, yes, sorry. Freudian slip. :-)

> > Besindes, of course, the naming (A::B vs C)?
>
> That's the main difference. Constant lookups are modified, e.g.
> referring to 'D' inside B will look for A::B::D, A::D or ::D in that
> order (I believe)

Thank you for your answer! That clarifies a lot.

Modules are used for mixins. Is it possible -- and does it make sense at all
- -- to mixin a class into another class? I guess not?

TIA,
Eric
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkwfZSgACgkQhS0drJ3goJITEwCfcP4Xu+f7r0ft1S+A4AVQAODu
YFEAn3a0qAwfspUpZ88+y/CQPEnpOuBb
=HV2R
-----END PGP SIGNATURE-----

From: Brian Candler on
Eric MSP Veith wrote:
> Modules are used for mixins. Is it possible -- and does it make sense at
> all
> - -- to mixin a class into another class? I guess not?

$ irb --simple-prompt
>> class A;end
=> nil
>> class B;include A;end
TypeError: wrong argument type Class (expected Module)
from (irb):2:in `include'
from (irb):2
--
Posted via http://www.ruby-forum.com/.

From: Eric MSP Veith on
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Monday 21 June 2010, Brian Candler <b.candler(a)pobox.com> wrote:
> TypeError: wrong argument type Class (expected Module)
> from (irb):2:in `include'
> from (irb):2

Question answered. :-) Thank you!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkwfl1IACgkQhS0drJ3goJJmbwCfRMKNXgyrbfXDYdPY3v1h/xkJ
fUoAnRMon1tBVTH6s5SBfSubBSkeKIlL
=tjTq
-----END PGP SIGNATURE-----