[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

using a module at the toplevel doesn't work

Trans

8/17/2006 1:15:00 PM

Why? Becuase the module uses #define_method, and #define_method isn't
defined in main.

Which brings me to two questions:

1. How do I get around this problem? Please save me from having to
delgate the whole module!

2. Why isn't main a self extended module to begin with? Honestly, this
question has been buigging me for a long time.

T.

30 Answers

Logan Capaldo

8/17/2006 1:35:00 PM

0


On Aug 17, 2006, at 9:20 AM, Trans wrote:

> Why? Becuase the module uses #define_method, and #define_method isn't
> defined in main.
>
> Which brings me to two questions:
>
> 1. How do I get around this problem? Please save me from having to
> delgate the whole module!
>
irb(main):006:0> case self
irb(main):007:1> when Module, Class
irb(main):008:1> define_method(:a) { 1 }
irb(main):009:1> else
irb(main):010:1* (class << self; self; end).class_eval
{ define_method(:a) { 1 } }
irb(main):011:1> end


> 2. Why isn't main a self extended module to begin with? Honestly, this
> question has been buigging me for a long time.
>
Couldn't tell you.

> T.
>
>


Trans

8/17/2006 3:08:00 PM

0


Logan Capaldo wrote:
> On Aug 17, 2006, at 9:20 AM, Trans wrote:
>
> > Why? Becuase the module uses #define_method, and #define_method isn't
> > defined in main.
> >
> > Which brings me to two questions:
> >
> > 1. How do I get around this problem? Please save me from having to
> > delgate the whole module!
> >
> irb(main):006:0> case self
> irb(main):007:1> when Module, Class
> irb(main):008:1> define_method(:a) { 1 }
> irb(main):009:1> else
> irb(main):010:1* (class << self; self; end).class_eval
> { define_method(:a) { 1 } }
> irb(main):011:1> end

That's what I want to avoid -- "delegating the whole module" :-(

> > 2. Why isn't main a self extended module to begin with? Honestly, this
> > question has been buigging me for a long time.
> >
> Couldn't tell you.

Matz?

T.

Trans

8/17/2006 3:33:00 PM

0


Trans wrote:
> Logan Capaldo wrote:
> > On Aug 17, 2006, at 9:20 AM, Trans wrote:
> >
> > > Why? Becuase the module uses #define_method, and #define_method isn't
> > > defined in main.
> > >
> > > Which brings me to two questions:
> > >
> > > 1. How do I get around this problem? Please save me from having to
> > > delgate the whole module!
> > >
> > irb(main):006:0> case self
> > irb(main):007:1> when Module, Class
> > irb(main):008:1> define_method(:a) { 1 }
> > irb(main):009:1> else
> > irb(main):010:1* (class << self; self; end).class_eval
> > { define_method(:a) { 1 } }
> > irb(main):011:1> end
>
> That's what I want to avoid -- "delegating the whole module" :-(

Craggy. It's even worse than that! If I delegate via the singelton of
main then:

class << self
def x; "x"; end
end

class C
def q; x; end
end

C.new.q
=> NameError: undefined local variable or method `x' for
#<C:0xb7cfffc4>

(*frustrated*) It's not the same as defining at the top level.

T.

Yukihiro Matsumoto

8/17/2006 4:54:00 PM

0

Hi,

In message "Re: using a module at the toplevel doesn't work"
on Thu, 17 Aug 2006 22:20:06 +0900, "Trans" <transfire@gmail.com> writes:

|Why? Becuase the module uses #define_method, and #define_method isn't
|defined in main.
|
|Which brings me to two questions:
|
|1. How do I get around this problem? Please save me from having to
|delgate the whole module!
|
|2. Why isn't main a self extended module to begin with? Honestly, this
|question has been buigging me for a long time.

I don't think I understand you. Can you elaborate?

matz.

Pit Capitain

8/17/2006 4:56:00 PM

0

Trans schrieb:
> Craggy. It's even worse than that! If I delegate via the singelton of
> main then:
>
> class << self
> def x; "x"; end
> end
>
> class C
> def q; x; end
> end
>
> C.new.q
> => NameError: undefined local variable or method `x' for
> #<C:0xb7cfffc4>
>
> (*frustrated*) It's not the same as defining at the top level.

Tom, defining a method at the top level is the same as defining a
private method of Object:

def m
end

is the same as

class Object
private
def m
end
end

What are you trying to do?

Regards,
Pit

Trans

8/17/2006 5:35:00 PM

0


Yukihiro Matsumoto wrote:

> I don't think I understand you. Can you elaborate?

Sure thing. It seems natural to me that "main" would a module of the
form:

module Main
extend self
end

Such a module provides all the characteristics of the toplevel --def,
include, etc.

Hmmm.... now that I spell it out.... Given that every method defined in
main becomes a private method of Object, this Main object looks a whole
lot like Kernel itself. So maybe that's a better way to think about it:
Why isn;t the toplevel Kernel (w/toplevel methods being private methods
of Kernel)?

T.

Trans

8/17/2006 5:45:00 PM

0

Hi Capitain,

> What are you trying to do?

Well, I have module called Taskable. It's an emulation of Rake's basic
task pattern. eg.

desc "foo description"

task :foo => [ :foo_prerequisite ] do
...
end

But rather than define tasks globally as with Rake's system. I designed
it to work within modules/classes namspace including the tasks being
actual methods. So the one can do:

class X
task :a => [:b] do
print "a"
end

task :b do
print "b"
end
end

X.new.a; puts
=> ba

Unfortuately when I use this to make a Rake command-line emulator I
want to use the module at the toplevel and can't.

Thanks,
T.

Yukihiro Matsumoto

8/17/2006 5:51:00 PM

0

Hi,

In message "Re: using a module at the toplevel doesn't work"
on Fri, 18 Aug 2006 02:40:05 +0900, "Trans" <transfire@gmail.com> writes:

|Yukihiro Matsumoto wrote:
|
|> I don't think I understand you. Can you elaborate?
|
|Sure thing. It seems natural to me that "main" would a module of the
|form:
|
| module Main
| extend self
| end
|
|Such a module provides all the characteristics of the toplevel --def,
|include, etc.

Why? If it is really required it's fairly easy to add toplevel
methods like we did for #include.

matz.

Trans

8/17/2006 6:24:00 PM

0


Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: using a module at the toplevel doesn't work"
> on Fri, 18 Aug 2006 02:40:05 +0900, "Trans" <transfire@gmail.com> writes:
>
> |Yukihiro Matsumoto wrote:
> |
> |> I don't think I understand you. Can you elaborate?
> |
> |Sure thing. It seems natural to me that "main" would a module of the
> |form:
> |
> | module Main
> | extend self
> | end
> |
> |Such a module provides all the characteristics of the toplevel --def,
> |include, etc.
>
> Why? If it is really required it's fairly easy to add toplevel
> methods like we did for #include.

See what I wrote to Pit Capitain. Even if I define the missing toplevel
methods:

def define_method( meth, &block )
Object.class_eval do
define_method( meth, &block )
private meth
end
end

def ancestors
Object.ancestors
end

include MyModule

I still get errors because state (e.g. instance vars used in MyModule)
are being stored in the toplevel instance of Object, main, and not in
Object. I got methods going one way and instance vars going another.

Is there a reason the toplevel _can't_ be Kernel instead of a specal
instance of Object? Just seems like that would be hek of a lot easier
all around.

Thanks,
T.

Pit Capitain

8/17/2006 7:25:00 PM

0

Trans schrieb:
> Unfortuately when I use this to make a Rake command-line emulator I
> want to use the module at the toplevel and can't.

Tom, I think I still don't get what your problem is. If you want to use
a module at the toplevel just include it in Object.

The module:

module M
def val *args
if args.empty?
@val
else
@val = args[ 0 ]
end
end
end

I used some instance variables here, because you wrote about them in the
answer to Matz.

Make it available at the toplevel (and everywhere else):

class Object
include M
end

p val # => nil
val "main"
p val # => "main"

It's also available at the class level:

class Q
p val # => nil
val "Q"
p val # => "Q"
end

HTH

Regards,
Pit