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