S
Simon Kitching
Hi,
I'm writing a Ruby app at the moment, and have some questions I hope you
can help me with:
(a) string->class
I have a string containing the name of a class, and want to get the
corresponding Class object. My current approach is:
Module.class_eval(classname)
Is this the best approach?
(b) changing "require" searchpath
The $: variable holds the path used to locate files when the "require"
command is used. However this variable is read-only. In Perl, it is
possible to modify the equivalent path; can this be done in ruby?
The reason I want this is that I want to run my test harness from one
directory, and pick up my in-development code from some other directory.
Using "require '../xmldigester.rb'" doesn't work, because all the
requires in that required file resolve against $:.
(c) how invoke "hidden" methods?
I have a class Digester which responds to SAX events generated by REXML.
However I don't want methods like "start_element" to be public on
Digester, because this is just untidy: these methods are "internal" to
the Digester class, and should not be available for ordinary users of
this class.
In Java, I would declare package-scope methods on the Digester class,
then write an adapter class which implements SaxContentHandler, and
forwards calls to its methods to the appropriate Digester object
methods.
However Ruby doesn't have "package-scope" methods. Any idea how I
achieve a similar effect in Ruby?
(d) iterating in reverse
I have an Array object, and wish to iterate over its members in reverse.
I would prefer not to have to create a new Array object in order to do
this iteration. Is there any way other than writing
for(i=array.length-1; i>=0; --i) ....
Thanks,
Simon
I'm writing a Ruby app at the moment, and have some questions I hope you
can help me with:
(a) string->class
I have a string containing the name of a class, and want to get the
corresponding Class object. My current approach is:
Module.class_eval(classname)
Is this the best approach?
(b) changing "require" searchpath
The $: variable holds the path used to locate files when the "require"
command is used. However this variable is read-only. In Perl, it is
possible to modify the equivalent path; can this be done in ruby?
The reason I want this is that I want to run my test harness from one
directory, and pick up my in-development code from some other directory.
Using "require '../xmldigester.rb'" doesn't work, because all the
requires in that required file resolve against $:.
(c) how invoke "hidden" methods?
I have a class Digester which responds to SAX events generated by REXML.
However I don't want methods like "start_element" to be public on
Digester, because this is just untidy: these methods are "internal" to
the Digester class, and should not be available for ordinary users of
this class.
In Java, I would declare package-scope methods on the Digester class,
then write an adapter class which implements SaxContentHandler, and
forwards calls to its methods to the appropriate Digester object
methods.
However Ruby doesn't have "package-scope" methods. Any idea how I
achieve a similar effect in Ruby?
(d) iterating in reverse
I have an Array object, and wish to iterate over its members in reverse.
I would prefer not to have to create a new Array object in order to do
this iteration. Is there any way other than writing
for(i=array.length-1; i>=0; --i) ....
Thanks,
Simon