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.
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
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:
dotest': undefined methodnew’ for Connector:Module
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:
First of all, conventionally semicolons are only used when you want
multiple expressions sharing the same line. They are completely
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
Methods in Ruby return the last value by default, so “return” here is
And some more:
A more idiomatic way to write this would be:
@soc.close unless @soc.closed?
And for this:
The Rubyish way would be:
Hope that helps.