Newbie needs help! Strange error in code

Hi, I am very new to Ruby and yesterday I managed to get a simple server
operating which told me the time. I tried to extend this by adding in a
simple myvar = gets.chomp! and a print myvar in place of the date
output. This is the whole code:

test = gets.chomp!
puts test
require ‘socket’
port = (ARGV[0] || 80).to_i
server = TCPServer.new(‘localhost’, port)
while (session = server.accept)
puts “Request: #{session.gets}”
session.print “HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n”
session.print “

#{test}

\r\n”
session.close
end

The problem is that I get and error when I run it which says “`gets’: No
such file or directory - 1400 (Errno::ENOENT)”

I run the program with “ruby x.rb 1400” 1400 is the port number

If anyone can help me, I will be eternally thankfully.

On 25 Jun 2007, at 16:31, Tom S. wrote:

port = (ARGV[0] || 80).to_i
such file or directory - 1400 (Errno::ENOENT)"

I run the program with “ruby x.rb 1400” 1400 is the port number

If anyone can help me, I will be eternally thankfully.


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

‘gets’ isn’t doing what you think it is:

------------------------------------------------------------ Kernel#gets
gets(separator=$/) => string or nil

  Returns (and assigns to +$_+) the next line from the list of files
  in +ARGV+ (or +$*+), or from standard input if no files are

present
on the command line. Returns +nil+ at end of file. The optional
argument specifies the record separator. The separator is included
with the contents of each record. A separator of +nil+ reads the
entire contents, and a zero-length separator reads the input one
paragraph at a time, where paragraphs are divided by two
consecutive newlines. If multiple filenames are present in +ARGV+,
+gets(nil)+ will read the contents one file at a time.

     ARGV << "testfile"
     print while gets

  _produces:_

     This is line one
     This is line two
     This is line three
     And so on...

By calling your script as ‘ruby x.rb 1400’ gets attempts to read the
first line of a file called ‘1400’, which doesn’t exist. If you run
the program as ‘ruby x.rb’ and then type 1400 when it pauses you will
get the behavior (I think) you expect. Alternatively replace
gets.chomp! with ARGV[0].chomp.

Alex G.

Bioinformatics Center
Kyoto University

Thanks very much! It solved my problem.