Unrequiring a module


    Sponsored Links


  • 1. ruby-dev summary 24236-24254
    Hello, Here is a summary of ruby-dev last week. [ruby-dev:24240] on enabling tcltk-stubs Hidetoshi Nagai, who is a maintainer of Ruby/Tk, called for help since a program abort on exit of Tcl/Tk interpreter as follows: $ /usr/local/bin/ruby -r tcltklib -e 'TclTkIp.new._eval("exit")' Tcl_Release couldn't find reference for 0x814ed38 A trial fix of this issue was done, and he asked for someone to check if the library works fine. Also, he asked for someone to try multi-tk.rb and remote-tk.rb, since those have been improved from the previous version. [ruby-dev:24250] DateTime#to_time Tadayoshi Funaba posted a specification and a prototype of following methods. - DateTime#to_time, #to_date - Date#to_time, #to_datetime - Time#to_date, #to_datetime Significant points are as follows: - It is not ensured that yyy.to_xxx.to_yyy is equal to yyy. - to_time * It always returns local time. * It may not return intended result because of summer time and leap second. * 'usec' is rounded. - to_date and to_datetime * Time zone information is lost, but time difference is left on DateTime. * Leap time is not considerable. -- Takaaki Tateishi < XXXX@XXXXX.COM >
  • 2. Ruby and LD_RUN_PATH
    I'm trying to distribute a version of Ruby along with an application, and have a dependency on several shared objects that need to be distributed with it. On Windows, this is no problem: just add the DLLs to the 'bin' directory from which Ruby is run, and the system picks them up and loads them as needed. Under Linux, though, I seem to be completely unable to get Ruby to recognize the libraries. My file tree looks something like this: app/ (my sources and data files go here) ruby/ bin/ ruby lib/ <somelib>.so ruby/ 1.8/ ... I'm setting LD_RUN_PATH to './ruby/lib', and then launching the Ruby binary with the flags '-I./ruby/lib/ruby/1.8 -I./ruby/lib/ruby/1.8/i686-linux'. It finds the Ruby libraries and shared object files just fine, but the <somelib>.so is not being picked up. Any ideas? -- Lennon rcoder.net
  • 3. Truly modular
    Hi, As Ruby claims to be very modular I'd like to do the following (I tried some things but I think I'm going in the wrong directions). I have my main Ruby application. The applications need to be extendible. My idea is to write extra .rb files and put them in one directory. The main app should somehow load those files to extend functionality. on runtime the files can changes and the main app should be able to reload the extensions. Also new files can be added and these should be reloaded too. I don't know the right approach to this problem. The external .rb files somehow need to have access to parts of the main application and the main application obviously needs to call functions from the external files. Users can communicatie with the main app telling it there are new external files to be loaded, the name of the files and maybe function names to be called or whatever. The idea is that I don't want to restart the main application but do want to extend it on the flow. Somebody with some great ideas how to achieve this? Jesse van den Kieboom
  • 4. closing all open file descriptors
    > all i can think now is > > 3.upto(65536){|fd| IO::new(fd).close rescue nil} > > which is incredibly expensive! That's how it's usually done in C, though often only to 256. If it's inefficient, a File.close_all(from) method might be nice as a C extension. Ari

Unrequiring a module

Postby Iain Dooley » Sun, 28 Aug 2005 21:55:28 GMT

hello all, i'm using:


to add a constant (class name) to my namespace in an embedded Ruby 
application. If i want to change the implementation details of that 
class without restart my application, how can i unrequire and re-require 
that file?

thanks very much

Iain Dooley

Re: Unrequiring a module

Postby Austin Ziegler » Sun, 28 Aug 2005 22:20:06 GMT

Strictly speaking, you can't. Well, you can't unrequire. However,
since all classes in Ruby are open, you can simply do the C-side
equivalent of Kernel.load.

Austin Ziegler *  XXXX@XXXXX.COM 
               * Alternate:  XXXX@XXXXX.COM 

Re: Unrequiring a module

Postby Iain Dooley » Sun, 28 Aug 2005 22:28:07 GMT

hi Austin, thanks very much for your speedy response

my worry with doing this would be that it would reload the 'Qt' 
extension that i'm including as well, which takes a while and is done on 

i've gotten around the problem by replacing my call to 
rb_require(filename) with:


i read here:


that the difference between load and require in a Ruby script is that 
load will load the file everytime where require will do it only once. is 
there any reason why rb_load_file does not parse the document into the 
Ruby namespace? my ruby file looks like this:

class AssignSomeText < Qt::Object
     slots 'buttonClicked()'

     def setParent(parent)
         @parent = parent

     def buttonClicked()
         @parent.child("textLabel1").text = @parent.child("lineEdit1").text

if i simply load this file, then AssignSomeText is an undefined 
constant. if i rb_require() it then AssignSomeText is defined, but if i 
issue the require command again have changed some implementation 
details, those changes are not reflected unless i actually restart the 
application that the ruby interpreter is embedded in



Re: Unrequiring a module

Postby ts » Sun, 28 Aug 2005 22:42:18 GMT

>>>>> "I" == Iain Dooley < XXXX@XXXXX.COM > writes:

I>      rb_load_file(filename);

I> class AssignSomeText < Qt::Object

 first remove the constant AssignSomeText, then use rb_load_protect()

Guy Decoux

Re: Unrequiring a module

Postby Iain Dooley » Sun, 28 Aug 2005 22:45:14 GMT

i tried to find some way of remove a constant, but couldn't see one, 
what is the method?



Re: Unrequiring a module

Postby ts » Sun, 28 Aug 2005 22:53:05 GMT

>>>>> "I" == Iain Dooley < XXXX@XXXXX.COM > writes:

I> i tried to find some way of remove a constant, but couldn't see one, 
I> what is the method?


 rb_mod_remove_const() (in variable.c)

Guy Decoux

Similar Threads:

1.Unloading (or unrequiring) a file

Hey all,

I'm trying to dynamically load parts of code to make things and  
methods available.

The catch is that I'd also like to make those things and methods  
UNavailable as well (or load something else in place of the original  
loaded item).

I know there's no Kernel#unload, so how can I achieve my goal?

Nietzsche is my copilot

2.Module#before implementation (was: problem with Module#append_features)

ts wrote:
>  No, this is because #initialize is a private method. Try it with
>  private_method_defined?

Well, it was still a pain, as the 'initialize' methods seems always be

fery@domesticus:~$ irb1.8
irb(main):001:0> class A
irb(main):002:1> puts "init?: #{private_method_defined?(:initialize)}"
irb(main):003:1> end
init?: true

So I decided to always re-hack a method when it is added to a class,
anyway, it is a logical thing to do.

Below is my solution, as this list do not accept even ascii attachments,
and wait for the simpler/shorter implementations, as I am always happy
to learn.

I hope the long rows will not break...


PS: I have looked aspectR, but its source was too long (9K :-), so I
didn't look into its implementation details.

---------------------------------------->8---- before.rb

#!/usr/bin/env ruby
def debug(*args)

# Extension of Module to define methods in modules which run before the 
# appropriate method in the class which mixes the module.
class Module
  def before_init
    @before = []
  def before(*method_names)
    before_init() if !@before
    @before += method_names
    debug "#{self}.@before==#{@before}", :before
  def before_get_before() @before end
  def before_check_new_method(mod,aSymbol)
    if !(mod.before_get_before.nil?) && (mod.before_get_before.include?
      debug "#{self}.before_check_new_method(#{mod},#{aSymbol}):
#{aSymbol} is in #{mod}.@before.", :before
      debug "Executing
beforehack_insertBeforeWrapper(#{self},#{aSymbol})", :before
  alias pre_beforehack_append_features append_features
  def append_features(klass)
    before_init() if !@before
    @before.each {|method_name|
      #~ if klass.method_defined?(method_name) ||
klass.private_method_defined?(method_name) # Only with ruby 1.8
      if klass.method_defined?(method_name) ||
klass.private_instance_methods.include?(method_name.id2name) # The
method is already defined 
        debug "#{self}.append_features(#{klass}): checking method
'#{method_name}': exists.", :before
        debug "Executing
beforehack_insertBeforeWrapper(#{klass},#{method_name})", :before
        debug "#{self}.append_features(#{klass}): checking method
'#{method_name}': do not exist.", :before
  def beforehack_insertBeforeWrapper(klass,method_name)
    debug "beforehack_insertBeforeWrapper: hacking
#{klass}.#{method_name} to exec #{method_name}_#{self}", :before
    klass.module_eval <<-END_OF_ADVICE
      alias pre_include_#{self}_#{method_name} #{method_name}
      def #{method_name}(*args,&block)
  alias pre_beforehack_method_added method_added if self.respond_to?
  def method_added(aSymbol)
    if !@before_in_beforehack_semaphore
      self.included_modules.each {|mod|
    self.pre_beforehack_method_added if self.respond_to?

# Usage example:

module Mod
  before :fnname
  def fnname_Mod
    puts "fnname_Mod called"

class Klass
  include Mod
  def fnname
    puts "fnname called"


3.YAML-ing classes and modules in 1.8.4 -- Was: Extend an object with module stored

Joel VanderWerf wrote:
> I had some code to serialize module and class identities (without any of
> their instance variables or class variables, though) with 1.8.2 (see
> below) but it seems not to work with 1.8.4. I will take a look at it
> later today.

Here's a version for 1.8.4. Are you listening, _why, and is there a
chance of putting this in the YAML lib?

Sample code:
yy = [Enumerable, Comparable, String, File].to_yaml
puts yy
p YAML.load(yy)

- !ruby/module Enumerable
- !ruby/module Comparable
- !ruby/class String
- !ruby/class File
[Enumerable, Comparable, String, File]

require 'yaml'

class Module
  yaml_as "tag:ruby.yaml.org,2002:module"

  def Module.yaml_new( klass, tag, val )
    if String === val
      val.split(/::/).inject(Object) {|m, n| m.const_get(n)}
      raise YAML::TypeError, "Invalid Module: " + val.inspect

  def to_yaml( opts = {} )
    YAML::quick_emit( nil, opts ) { |out|
      out.scalar( "tag:ruby.yaml.org,2002:module", self.name, :plain )

class Class
  yaml_as "tag:ruby.yaml.org,2002:class"

  def Class.yaml_new( klass, tag, val )
    if String === val
      val.split(/::/).inject(Object) {|m, n| m.const_get(n)}
      raise YAML::TypeError, "Invalid Class: " + val.inspect

  def to_yaml( opts = {} )
    YAML::quick_emit( nil, opts ) { |out|
      out.scalar( "tag:ruby.yaml.org,2002:class", self.name, :plain )

      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

4.Module nesting and module vs. instance methods

Having made the commitment to ruby, I am now finding myself building
bigger frameworks and ruby's limited namespace rules with modules are
potentially showing some issues that I want to either prepare for or
Or perhaps it is not entirely clear to me what's the proper Ruby way of
doing it.  Basically I am building some libraries and I am trying to be
careful about name pollution.

I have a bunch of functions that, currently, have no particular class
location.  Currently, I am placing them in modules.
As I have a bunch of them that are somewhat unrelated, I have been
creating modules following the standard procedure I've used in other
languages.  That is, I'm using:

module MyCompany
    module Module
         def self.func1
         def  func2

Now... the problems I am facing are...

1) Ruby's name pollution on includes.

--- B ---
module B
   def bfunc
      puts "bfunc"
--- A ---
require 'B'

module A
   def afunc
      puts "afunc"
---- some other code
require "A"
class X
   def initialize
      afunc   # this should work
      bfunc   # this should fail
I want to have code that includes A, but does not include B.  However,
class X should not have access to functions in module B, which it never

How can I safely work around this?  I want to avoid behaviors or
anything like that.

2) I also would like to have some modules that work either as include
modules or as modules with stand-alone methods.  Problem is that ruby
will not "include" class methods.
That is, say I have a funcion called runcmd() to run a shell command
and do stdin/out/err piping.  This function is rather common, so I
would like to have:

module Shell
    def self.runcmd(cmd)
       # ....do tons of stuff
    def runcmd(cmd)

So I can do:



class Backup
     include Shell
     def doit

The above works but it seems kind of silly and wasteful for something
that to me seems pretty common (not to mention that the instance method
access will likely be slower, too).

5.Module#dup and Module.remove_method question


I have a question about Module#dup and Module.remove_method. 

Say I have the following:

 > module A
 >  def self.foo; puts "A::foo; end
 > end

 > B = A.dup
 > B.foo 
 => nil

But now I want to remove foo from A's duplicate:

 > class <<B; remove_method :foo; end
   NameError: method `foo' not defined in Module
         from (irb):37:in `remove_method'
         from (irb):37

Ah? Well OK, let's remove it from A then.

 > class <<A; remove_method :foo; end
 => #<Class:A>

Let's test it:

 > A.foo
 NoMethodError: undefined method `foo' for A:Module
        from (irb):40

That works. BUT:

 > B.foo
 => nil

This, I do not understand: where does B.foo live, now and why can't I
remove it?



6. include Module in another Module

7. Using object methods of first module in methods of second module

8. Module.nesting for anonymous modules?

Return to ruby


Who is online

Users browsing this forum: No registered users and 61 guest