uninitialized constant Connector(Name Error)

T

Tay Thotheolh

I have seen a thread that has nearly the same problems but I guess mine
should be some other problems.

I had my Connector.rb and test.rb classes next to each other so it
should be able to interact.

Do bear with me as I am very new to ruby. I just started to experiment
abit with ruby lately.

Attachments:
http://www.ruby-forum.com/attachment/5042/problem.zip
 
J

Justin Collins

Is this problem really that uncommon ?

Hi Tay,

For future questions, please describe the results you are trying to
achieve and how what you see differs from what you want. Include error
messages if they exist. If your code is not too long (and in this case
it really isn't) it is easier for people to help if it is directly
included in your email.

It appears that you are expecting Ruby to "know" that the Connector
class (really the Connector constant) is defined in the Connector.rb
file (in a Java-ish) way. That's not how Ruby works. You need to
explicitly "load" or "require" a file. For example, add

require "connector" #if you are using Ruby 1.8

or

require_relative "connector" #if you are using 1.9.2

to the top of your test.rb file.

However, you will quickly find that you get a new error. Something like:

test.rb:13:in `dotest': undefined method `new' for Connector:Module
(NoMethodError)
from test.rb:28:in `<main>'

I think you want Connector to be a class, not a module. Modules are not
instantiated as instances, only classes are.

Okay. Now your code will likely work, but there are still some issues.
In Connector.rb (by the way, the convention would be to have the file
name lower-cased), you have this:

#Globals
@hostname;
@port;
@soc;

First of all, conventionally semicolons are only used when you want
multiple expressions sharing the same line. They are completely
redundant here.

Secondly, these are not really globals. Globals begin with a dollar sign
($). But why even bother? You do not use these as globals. You can get
rid of these lines completely.

Here's some more code:

def send(message) # send tcp messages
@soc.puts(message);
return @soc.gets;
end

Methods in Ruby return the last value by default, so "return" here is
not needed.

And some more:

def close()

if(!(@soc.closed?)==true)
@soc.close
end

end

A more idiomatic way to write this would be:


def close

@soc.close unless @soc.closed?

end

And for this:

def is_closed()
return @soc.closed?
end

The Rubyish way would be:

def closed?
@soc.closed?
end


Hope that helps.

-Justin
 
T

Tay Thotheolh

Hi Collins. Thanks for the reply. Would look into it.
file (in a Java-ish) way

I came from a Java background where my idea was you just call Connector
like in Java and it should be loaded. This is my first attempt of really
sitting down and creating a ruby program by trial and error and learning
along the way.
 

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

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top