# Newbie needs help! Strange error in code

**URL:** <https://www.ruby-forum.com/t/newbie-needs-help-strange-error-in-code/107683>\
**Category:** Ruby\
**Created:** [June 25, 2007, 9:31am UTC](https://www.ruby-forum.com/t/newbie-needs-help-strange-error-in-code/107683 "2007-06-25T09:31:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [June 25, 2007, 9:31am UTC](https://www.ruby-forum.com/t/newbie-needs-help-strange-error-in-code/107683/1 "2007-06-25T09:31:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [June 25, 2007, 9:57am UTC](https://www.ruby-forum.com/t/newbie-needs-help-strange-error-in-code/107683/2 "2007-06-25T09:57:45Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [June 25, 2007, 11:33am UTC](https://www.ruby-forum.com/t/newbie-needs-help-strange-error-in-code/107683/3 "2007-06-25T11:33:37Z")

</div>

Thanks very much! It solved my problem.
