[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: 13 lines of confusion

Rick DeNatale

5/21/2008 2:35:00 AM

On Tue, May 20, 2008 at 10:02 PM, Michael T. Richter
<ttmrichter@gmail.com> wrote:
> I can't for the life of me figure out why the following code makes Ruby puke
> when I try to execute it:
>
> module Rlang
> module Error
> class RlangException < Exception ; end
> end
> module Utility
> def e8(value) ; [value].pack('C') ; end
> end
> module External
> include Rlang::Error
> include Rlang::Utility
> TAG_CACHED_ATOM = e8(67)
> end
> end
>
> The precise error I get is "junk.rb:11: undefined method `e8' for
> Rlang::External:Module (NoMethodError)" which is the line with
> "TAG_CACHED_ATOM = ..." in it.

the include Rlang::Utility causes the External module to define the e8
method for classes which include the External module either directly
or indirectly (e.g. through an included module which includes
External) but the line

TAG_CACHED_ATOM = e8(67)

is evaluated in the context of the module itself, self is External,
and the External object doesn't have this method.

Now if you change

include Rlang::Utility
to
extend Rlang::Utility

the file will run without error.

If you want classes which include External to have e8 as an instance
method as well you can both include and extend Rlang::Utility

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...