Accessing Kernel namespace from within classes...

  • Thread starter Gabriel Dragffy
  • Start date
G

Gabriel Dragffy

Hi all

Hoping someone could tell me the answer to the following.

I have a class defined in a file: TestWebsite. The main program
requires the 'net/http' library and also the TestWebsite file. The
class "TestWebsite" contains several class methods that use things
such as "Net::HTTP.get_response()" however I get the following errors:

/net/http.rb:380:in `get_response': undefined method `request_uri'
for #<URI::Generic:0x2d3b9c URL:example.com> (NoMethodError)
from /opt/local/lib/ruby/1.8/net/http.rb:543:in `start'
from /opt/local/lib/ruby/1.8/net/http.rb:379:in `get_response'
from ./test_website.rb:10:in `test'

It's my guess the the class methods aren't able to access the net/
http library which is in the kernel namespace, if this is true then
what must I do to allow them access?

Many thanks,

Gabriel
 
M

Matt White

Hi all

Hoping someone could tell me the answer to the following.

I have a class defined in a file: TestWebsite. The main program
requires the 'net/http' library and also the TestWebsite file. The
class "TestWebsite" contains several class methods that use things
such as "Net::HTTP.get_response()" however I get the following errors:

/net/http.rb:380:in `get_response': undefined method `request_uri'
for #<URI::Generic:0x2d3b9c URL:example.com> (NoMethodError)
from /opt/local/lib/ruby/1.8/net/http.rb:543:in `start'
from /opt/local/lib/ruby/1.8/net/http.rb:379:in `get_response'
from ./test_website.rb:10:in `test'

It's my guess the the class methods aren't able to access the net/
http library which is in the kernel namespace, if this is true then
what must I do to allow them access?

Many thanks,

Gabriel

Are you using a complete URI or just part of one? For example,
'domain.com' is not a URI. Further:

[root@server15 mwhite]# ruby -r uri -e 'p
URI.parse("google.com").host'
nil
[root@server15 mwhite]# ruby -r uri -e 'p URI.parse("http://
google.com").host'
"google.com"

If that's not the problem, maybe you could give us some code? Good
luck!

Matt
 
G

Gabriel Dragffy

Are you using a complete URI or just part of one? For example,
'domain.com' is not a URI. Further:

[root@server15 mwhite]# ruby -r uri -e 'p
URI.parse("google.com").host'
nil
[root@server15 mwhite]# ruby -r uri -e 'p URI.parse("http://
google.com").host'
"google.com"

If that's not the problem, maybe you could give us some code? Good
luck!

Oh thank you so much, you spotted that one a million miles off, I
missed out the http://. Have corrected this but now getting errors like:
/test_website.rb:13:in `test': uninitialized constant
TestWebsite::NET (NameError)
MMM something to keep my mind ticking over.

Many thanks

Gabriel
 
M

Matt White

Are you using a complete URI or just part of one? For example,
'domain.com' is not a URI. Further:
[root@server15 mwhite]# ruby -r uri -e 'p
URI.parse("google.com").host'
nil
[root@server15 mwhite]# ruby -r uri -e 'p URI.parse("http://
google.com").host'
"google.com"
If that's not the problem, maybe you could give us some code? Good
luck!

Oh thank you so much, you spotted that one a million miles off, I
missed out the http://. Have corrected this but now getting errors like:
/test_website.rb:13:in `test': uninitialized constant
TestWebsite::NET (NameError)
MMM something to keep my mind ticking over.

Many thanks

Gabriel

No problem. If you need more help, be sure to give us the code that is
causing the error and not just the error.
 
G

Gary Wright

Oh thank you so much, you spotted that one a million miles off, I
missed out the http://. Have corrected this but now getting errors
like:
./test_website.rb:13:in `test': uninitialized constant
TestWebsite::NET (NameError)
MMM something to keep my mind ticking over.

You can force constants to be evaluated from the
top-level rather than from the current lexical scope by
prefixing the constant with '::'

class TestSuite
ok = ::Net::HTTPOK
end

Gary Wright
 
G

Gabriel Dragffy

Are you using a complete URI or just part of one? For example,
'domain.com' is not a URI. Further:
[root@server15 mwhite]# ruby -r uri -e 'p
URI.parse("google.com").host'
nil
[root@server15 mwhite]# ruby -r uri -e 'p URI.parse("http://
google.com").host'
"google.com"
If that's not the problem, maybe you could give us some code? Good
luck!

Oh thank you so much, you spotted that one a million miles off, I
missed out the http://. Have corrected this but now getting errors
like:
/test_website.rb:13:in `test': uninitialized constant
TestWebsite::NET (NameError)
MMM something to keep my mind ticking over.

Many thanks

Gabriel

No problem. If you need more help, be sure to give us the code that is
causing the error and not just the error.

Thanks, Matt!
 
G

Gabriel Dragffy

You can force constants to be evaluated from the
top-level rather than from the current lexical scope by
prefixing the constant with '::'

class TestSuite
ok = ::Net::HTTPOK
end

Gary Wright

Thank you, Gary!
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top