The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Main is a DRY Pain

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Trans Onoma

Posts: 63
Nickname: transfire
Registered: Aug, 2005

Trans Onoma has programming for too long.
Main is a DRY Pain Posted: Aug 17, 2006 9:42 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Trans Onoma.
Original Post: Main is a DRY Pain
Feed Title: 7ransCode
Feed URL: http://feeds.feedburner.com/7ranscode
Feed Description: l33t c0d3 $p1n!
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Trans Onoma
Latest Posts From 7ransCode

Advertisement
I have a module that depends on define_method and ancestors. It works great when I include it in other modules or classes. But if I try including it into the toplevel it fails miserably.


NameError: undefined local variable or method `define_method' for main:Object
NameError: undefined local variable or method `ancestors' for main:Object


That seems fauirly peculiar when you consider that defining methods at toplevel is perfectly acceptable. One must then wonder, what is this toplevel thing anyway?


self #=> main


Okay, it calls itself "main". Great. But that doesn't really tell us anything. Let's check it's class:


self.class #=> Object


Ah. So it's an instance of Object. An instance of Object!? How is that possible? A normal instance of Object and main can't be exactly the same. Indeed, they are not.


(public_methods - Object.public_instance_methods).sort
=> ["include", "private", "public"]
singleton_methods
=> ["include", "private", "public", "to_s"]


Notice include has been defined here specifically for main. So something special's going on when you include a module into the toplevel. Hmm... What about methods defined at the toplevel? If main is an instance of Object then are they singleton methods? Well, no. Turns out they get "magically" defined as private methods of the Object class itself, and main's singleton class space is actually something else entirely.


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

class Q
def q; x; end
end

Q.new.q
=> NameError: undefined local variable or method `s' for #<Q:0xb7ce9a3c>


Which means the sure solution for my problem...


module Kernel
include MyModule
end


Can you guess?


module M
def m; "m"; end
end

module Kernel
include M
end

m
=> NameError: undefined local variable or method `m' for main:Object

class Q
def q; m; end
end

Q.new.q
=> NameError: undefined local variable or method `m' for #<Q:0xb7cbb324>


Lands me squre in the face of the Module Inclusion Problem.

All this leads me to two points. First, I'm stuck! There seems to be no solution to my problem other than rewritting a second version of my module specifically for the toplevel. Talk about lack of DRY! And 2) Why in the world isn't main a self extended module?

Read: Main is a DRY Pain

Topic: Streamlined Framework: Domain Model Administration... for free Previous Topic   Next Topic Topic: full text searching for ri content

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use