[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6331] RE: Class Browser Questions

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-11-14 01:02:41 UTC
List: ruby-talk #6331
Jim queries:
> 1) What's the best way to convert from a class name string to a class
>    object.  I've been using Eval(class_name) and trapping any 
> NameErrors.
>    I could also search ObjectSpace looking for a match.  Is there a
>    better way?

I don't know about the best way, but here's a home-made way which avoids
ObjectSpace browsing (which is very slow when there's more objects).


  class Module
    def find_constants(type)
      constants().collect { |const| 
        const_get const 
      }.find_all { |const|
        const.type == type
      }
    end
    def classes
      find_constants(Class)
    end
    def modules
      find_constants(Module)
    end
    def find_class(name)
      klass = classes.find { |c| c.name == name }
      unless klass
        modules.find { |m| klass = m.find_class(name) }
      end
      klass
    end
  end

  module Foo
    class Foo
    end
  end

  ["Array", "Foo::Foo"].each do 
    |class_name|
    puts "\nTrying to find a class named #{class_name}"
    klass = Module.find_class(class_name)
    p klass
    p klass.type
    p klass.name
  end

Outputting:

  ruby -v gemfinder_examples.rb 
  ruby 1.6.2 (2000-10-16) [i686-linux]

   Trying to find a class named Array
  Array
  Class
  "Array"

  Trying to find a class named Foo::Foo
  Foo::Foo
  Class
  "Foo::Foo"

The recursion had to be introduced in order to search classes inside
namespaces, that is modules. 

> 2) I've been looking at singleton methods and noticed that for a class
>    object c
> 
>       c.singleton_methods == c.methods - c.superclass.methods
> 
>    Will this always be true for class objects?

I'm not following the logic of my example at all, but it's anyway an
counter-example:

  class Foo < Array
  end
  c = Foo
  p c.singleton_methods - (c.methods - c.class.superclass.methods)

And it's output:

  ruby -v gemfinder_examples_2.rb 
  ruby 1.6.2 (2000-10-16) [i686-linux]
  ["new"]
  false

> 3) I would like to be able to tie classes and methods to source
>    files.  Currently, I don't see a direct way of doing this.  The
...
>    that information.  Does this sound reasonable?

Does, to me. But I'm sure there's a better way (as usual :). Methods contain
source and line number information already (where do you think the source
and line number is invented when printing error messages). Classes might
have the same. But AFAIK, that information is part of the interpreter's
expression tree, and not accessible to Ruby code. I might be very wrong here
too.

> 4) What about reloading source files?  I would like to be able to
>    reload a source file to get the latest changes.  I suspect 
> that the 
>    naive approach might have problems.  Can source files be 
> arbitrarily 
>    reloaded?  How can I detect deleted methods?

I'm very interested in answers to this one! I'm actually in the midst of
redefinition of 'require' to allow reloads. 

Anyway, what I guess is that require maintains "feature-list" so it won't
reload features already loaded. Method load, on contrary, loads everytime.
So my idea was to redefine 'require' to provide information of required
source files, and then manually 'load' them when needed.

	- Aleksi

In This Thread

Prev Next