Uninitialized constant: String::StringIO

P

Patrick Li

Hi,
I'm having a hard time spotting my bug in this code.
When it runs it gives me:
uninitialized constant String::StringIO (NameError)

Am I missing something? StringIO is a global constant. Why can't ruby
find it?

class String
def self.fromURL(string)
string = string.string if string.is_a?(StringIO)
CGI.unescape(string)
end
end

Thanks a lot for your help
-Patrick
 
A

ara.t.howard

Hi,
I'm having a hard time spotting my bug in this code.
When it runs it gives me:
uninitialized constant String::StringIO (NameError)

Am I missing something? StringIO is a global constant. Why can't ruby
find it?

class String
def self.fromURL(string)
string = string.string if string.is_a?(StringIO)
CGI.unescape(string)
end
end

Thanks a lot for your help
-Patrick


require 'stringio'

a @ http://codeforpeople.com/
 
R

Robert Klemme

Hi,
I'm having a hard time spotting my bug in this code.
When it runs it gives me:
uninitialized constant String::StringIO (NameError)

Am I missing something? StringIO is a global constant. Why can't ruby
find it?

class String
def self.fromURL(string)
string = string.string if string.is_a?(StringIO)
CGI.unescape(string)
end
end

$ ruby -e 'p StringIO'
-e:1: uninitialized constant StringIO (NameError)

robert@fussel ~
$ ruby -r stringio -e 'p StringIO'
StringIO

robert@fussel ~
$

Kind regards

robert
 
P

Patrick Li

Thanks Ara,

Just out of curiosity, why is it that I need
require "stringio" when running as a script under Apache, but not when I
run under irb?

Is there a way to tell when I need to require a library and when I
don't? (ie. from documentation, etc...)
 
A

ara.t.howard

Thanks Ara,

Just out of curiosity, why is it that I need
require "stringio" when running as a script under Apache, but not
when I
run under irb?

Is there a way to tell when I need to require a library and when I
don't? (ie. from documentation, etc...)

irb simply had already required it. there is no general way to tell
but, in this case you could check $LOADED_FEATURES

or, more simply

require 'stringio' unless defined?(StringIO)

but the require never hurts so just do it anyhow

a @ http://codeforpeople.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top