which generates this error -
uninitialized constant SetLogLevel::Readline (NameError)
At a guess: you are running this before you have done require
‘readline’
The easiest solution is to put
require 'readline'
at the top of this source file. If it has already been required, this
will do nothing; but if it has not already been required, it will load
it in.
Two questions:
Is my class really so unaware of the calling program’s namespace (I
rather think yes).
I am not sure what you mean about this. Constants are searched for in
your own class, encoding classes/modules, and the top level.
You can say ::Readline instead of Readline if you mean “I must use the
one defined in the top level”, but this will only make a difference if
your class has its own Readline as well, in which case a bare ‘Readline’
will use the one within your class.
If so, what is the best way to make sure the class has access to the
libraries already loaded in the calling program?
require ‘filename_containing_library’ at the top of each source file
which makes use of the class(es) defined in that library.
Sorry - I thought I’d included enough to justify my questions. Here’s a
full source file, which produces the problem, minus all methods and
classes not relevant to the immediate problem -
P.S. It would be extremely helpful if you could boil this down to a very
short program which replicates the problem, and then post that program
in its entirety.
For example, when I write this:
---- 8< -----
foo.rb
def main
Foo.new.bar
end
class Foo
def bar
Readline::readline(“>>”)
end
end
%w(readline).each{ |lib| require lib }
main
---- 8< -----
and type “ruby foo.rb” then it runs perfectly. The question is, what’s
different between this and what you posted? Does this program run
correctly for you? If so, I suspect the problem lies somewhere outside
what you posted.
This whole process is extremely useful:
In the act of minimising the problem, you’ll probably find the
solution yourself.
Even if you don’t, then you have made it as easy as possible for
other people on the list to understand and/or replicate the problem, and
hence to find the solution.
The question then becomes simply: “The following piece of code raises
the exception shown. Why?” Since line numbers in the exception will
match up exactly to the posted code, an experienced coder will likely
see the problem at once.
Making life easy for other people makes them much more inclined to help
you (both now and in the future), as does demonstrating that you’ve done
your own homework. There’s a very good article on this topic at http://www.catb.org/~esr/faqs/smart-questions.html
Even if you don’t, then you have made it as easy as possible for
your own homework. There’s a very good article on this topic at How To Ask Questions The Smart Way
Regards,
Brian.
Brian, Thanks for your excellent recommendations. Though I did attempt
to present a succinct code excerpt containing my problem, I can see,
after reading your comments, that I could certainly have reduced it to
essentials, and did not. An excellent idea, and that’s how I’ll present
problems here in the future. Thanks for the correction!
And thanks for the link.
And thanks for answering my original question about classes’ having
knowledge of libraries loaded in the calling code.
t.
–
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< [email protected] >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)