Uninitialized constant Connector(Name Error)

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.

Is this problem really that uncommon ?

Tay Thotheolh wrote:

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.

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.

On 09/12/2010 06:20 PM, Tay Thotheolh wrote:

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

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 methodnew’ for Connector:Module
(NoMethodError)
from test.rb:28:in `’

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs