Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > HELP! New to Ruby and need to debug for work! Chomp! Error

Reply
Thread Tools

HELP! New to Ruby and need to debug for work! Chomp! Error

 
 
Doug Blackman
Guest
Posts: n/a
 
      01-27-2011
Hi,

I programmed years before with .Net, Java, etc. and haven't had any
exposure to Ruby yet. Since I have a background in CS, I can kinda
follow what's going on but I'm stuck with this.

I have taken over support for a website within our company that allows
users to submit requests. When they submit a request, they are sent an
email through our Exchange server with the request details. I'm getting
an error in the Ruby code that sends the emails. Any help would be
appreciated.

I've attached a copy of the error and the ruby code where the error is
happening.

Thanks

Attachments:
http://www.ruby-forum.com/attachment...r_doc_1-25.doc
http://www.ruby-forum.com/attachment..._mail_utils.rb


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Sam Duncan
Guest
Posts: n/a
 
      01-27-2011
So the gets called on your socket in readline is returning nil instead
of a string object, and getting #chomp! called on it?

def readline(f)
(l = f.gets).chomp!
puts "srv> " + l if $debug
l
end

What do you see if you change it to;

def readline(f)
l = f.gets
puts "srv> %s" % l if $debug
l.chomp!
l
end

?

Sam


On 28/01/11 10:11, Doug Blackman wrote:
> Hi,
>
> I programmed years before with .Net, Java, etc. and haven't had any
> exposure to Ruby yet. Since I have a background in CS, I can kinda
> follow what's going on but I'm stuck with this.
>
> I have taken over support for a website within our company that allows
> users to submit requests. When they submit a request, they are sent an
> email through our Exchange server with the request details. I'm getting
> an error in the Ruby code that sends the emails. Any help would be
> appreciated.
>
> I've attached a copy of the error and the ruby code where the error is
> happening.
>
> Thanks
>
> Attachments:
> http://www.ruby-forum.com/attachment...r_doc_1-25.doc
> http://www.ruby-forum.com/attachment..._mail_utils.rb
>
>
>


 
Reply With Quote
 
 
 
 
Doug Blackman
Guest
Posts: n/a
 
      01-27-2011
Thanks for replying Sam. I just tried it and got the same error
message.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Sam Duncan
Guest
Posts: n/a
 
      01-27-2011
Have you got the debug flag turned on, and does it output anything to
the console? I would expect it to say something like;

srv> nil

... in which case the socket gets method is returning nil for whatever
reason. If you just wanted to ignore it, you could have it return an
empty string or something if l == nil and see what breaks further down
the line. Maybe the socket connection is going away, or there is nothing
more to read off the socket? I'm not familiar enough with Ruby's socket
libraries or the Classes/ Methods therein sorry.

def readline(f)
l = f.gets
puts "srv> %s" % l if $debug
if l.kind_of?(String)
l.chomp!
end
l
end

Sam


On 28/01/11 11:26, Doug Blackman wrote:
> Thanks for replying Sam. I just tried it and got the same error
> message.
>
>


 
Reply With Quote
 
Chuck Remes
Guest
Posts: n/a
 
      01-27-2011

On Jan 27, 2011, at 3:11 PM, Doug Blackman wrote:

> Hi,
>
> I programmed years before with .Net, Java, etc. and haven't had any
> exposure to Ruby yet. Since I have a background in CS, I can kinda
> follow what's going on but I'm stuck with this.
>
> I have taken over support for a website within our company that allows
> users to submit requests. When they submit a request, they are sent an
> email through our Exchange server with the request details. I'm getting
> an error in the Ruby code that sends the emails. Any help would be
> appreciated.
>
> I've attached a copy of the error and the ruby code where the error is
> happening.


Take a look at the docs here:

http://ruby-doc.org/core/classes/IO.html#M000920

The call to "f.gets" is trying to read the next string from the IO object (in this case it's a TCPSocket). The docs say you'll get nil when you hit EOF. You can't call #chomp (or most other methods) on NilClass without it throwing up its hands and exiting.

I'm assuming you are getting EOF here because the socket connection has been interrupted or dropped. Perhaps a router or firewall change is related? Alternately, perhaps the peer (the guy on the other end of this socket) is sending malformed messages. This is hard to debug without knowing all of the pieces.

cr


 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      01-28-2011
[Note: parts of this message were removed to make it a legal post.]

On Thu, Jan 27, 2011 at 4:56 PM, Sam Duncan <(E-Mail Removed)> wrote:

> Have you got the debug flag turned on, and does it output anything to the
> console? I would expect it to say something like;
>
> srv> nil
>
>

nil.to_s returns an empty string, you probably want %p in your format string
(or just use inspect)

 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      01-28-2011
On Fri, Jan 28, 2011 at 5:11 AM, Doug Blackman <(E-Mail Removed)> wrote:

> http://www.ruby-forum.com/attachment...r_doc_1-25.doc
> http://www.ruby-forum.com/attachment..._mail_utils.rb


refine your code.

1 you assume that gets always return something other than nil
2 if you accept nils, then convert it first to string before chomping
it or whatever..

best regards -botp

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-28-2011
On Fri, Jan 28, 2011 at 5:03 AM, botp <(E-Mail Removed)> wrote:
> On Fri, Jan 28, 2011 at 5:11 AM, Doug Blackman <(E-Mail Removed)> wrote:
>
>> http://www.ruby-forum.com/attachment...r_doc_1-25.doc
>> http://www.ruby-forum.com/attachment..._mail_utils.rb

>
> refine your code.
>
> 1 you assume that gets always return something other than nil
> 2 if you accept nils, then convert it first to string before chomping
> it or whatever..


Even better: drop method readline completely and use a loop with block:

def send_mail(to, subj, msg, filename="<EMPTY>")

# establish socket
s = TCPSocket.new($host, $port)

ntlm = false

# handle host greeting
readline s
writeline s, "EHLO #{$host}"
s.each |line|
line.chomp!
ntlm ||= /^250-AUTH.+NTLM.*/ =~ line
break if /^250 / =~ line
end

Note also that you then must declare variable "ntlm" outside the loop body.

You also need to change the way you work with the socket because it
will only be properly closed if all goes well. Your current code will
leak open sockets in case of errors and may eventually run out of file
descriptors if this is running longer. You either need to do

# establish socket
s = TCPSocket.new($host, $port)
begin
...
ensure
s.close
end


or simply use TCPSocket.open with block:

TCPSocket.open($host, $port) do |s|
...
end

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Possibly a new ruby-debug release around the New Year r Ruby 0 12-18-2007 08:29 PM
debug="false" in web.config and <%@ debug="true" ...%> in aspx file => true or false? André ASP .Net 3 08-28-2006 12:02 PM
Config Mgr Debug/Release and Web.config Compilation debug=true RonL ASP .Net 0 04-08-2006 03:50 PM
Debug (DLL MFC) -> Debug (Static MFC) ringos75 C++ 0 04-14-2005 01:50 PM
[Howto] Compiling debug Python extensions for non-debug Python Mike C. Fletcher Python 3 10-12-2003 09:37 PM



Advertisments