Scope Missunderstanding

V

Vic P.h.

i got a module x and then a class z and inside this class i do a
Net::HTTP call and it looks for it inside module x ... and i am
clueless...

i get uninitialized constant ModuleX::Net::HTTP

i am new to ruby... and i just dont know how the scope works...
or what i am doing wrong

i took a look at the soap module
and it dosent look like it does anything
different than what i am doing...


code>
require 'uri'
require 'net/http'

module ModuleX

class Parser
def self.getData(d)
htmldata = self.loadData(d)
end
private
def self.loadData(profile)
url=''
htmldata = Net::HTTP.get(URI.parse(url))
htmldata.to_s
end
end

end

i advance thanks for shading liggt
 
G

Gary Wright

i got a module x and then a class z and inside this class i do a
Net::HTTP call and it looks for it inside module x ... and i am
clueless...

i get uninitialized constant ModuleX::Net::HTTP

I couldn't replicate that error using your code in ruby 1.8.7 or
in ruby 1.9. What version are you using?

In any case, you can always force constants to be resolved
from the top level by preceding them with two colons:

::Net::HTTP

Gary Wright
 
D

David A. Black

Hi --

i got a module x and then a class z and inside this class i do a
Net::HTTP call and it looks for it inside module x ... and i am
clueless...

i get uninitialized constant ModuleX::Net::HTTP

i am new to ruby... and i just dont know how the scope works...
or what i am doing wrong

i took a look at the soap module
and it dosent look like it does anything
different than what i am doing...


code>
require 'uri'
require 'net/http'

module ModuleX

class Parser
def self.getData(d)
htmldata = self.loadData(d)
end
private
def self.loadData(profile)

That private directive isn't doing what you think. It governs instance
methods, but you're defining class methods. If you want a private
class method, you need to declare it private in the class's singleton
class:

class C
private
def self.talk
puts "hi"
end
class << self
private
def talk_privately
puts "hi privately"
end
end
end

C.talk # "hi"
C.talk_privately # error
url=''
htmldata = Net::HTTP.get(URI.parse(url))
htmldata.to_s
end
end

end

Like Gary, I can't replicate the constant error you're getting.


David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
 
B

Brian Candler

Mex said:
i got a module x and then a class z and inside this class i do a
Net::HTTP call and it looks for it inside module x ... and i am
clueless...

i get uninitialized constant ModuleX::Net::HTTP

Almost certainly you forgot the "require 'net/http'" in the actualy code
you're using, or else you have a typo in the 'Net' or 'HTTP'.

Ruby tries both Net::HTTP and ModuleX::Net::HTTP, and will only give
this error if both don't exist. Hence I don't believe that you have
actually loaded Net::HTTP.
 

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,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top