# Beginners Question

**URL:** <https://www.ruby-forum.com/t/beginners-question/51316>\
**Category:** Ruby\
**Created:** [December 29, 2005, 4:18pm UTC](https://www.ruby-forum.com/t/beginners-question/51316 "2005-12-29T16:18:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jughead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@jughead](https://www.ruby-forum.com/u/jughead)\
**Post date:** [December 29, 2005, 4:18pm UTC](https://www.ruby-forum.com/t/beginners-question/51316/1 "2005-12-29T16:18:08Z")

</div>

Hello Folks,

I am trying to learn ruby, so i wrote some simple scripts.  
But the following script does not work as expected:

#!/usr/bin/ruby

while gets != nil  
print $\_  
end

if i call ‘echo.rb’ (without any parameters) from my cygwin shell, the  
scripts does what i expect it to do. It reads and returns the input.  
But if i call it with any parameters, like

echo.rb anything

i get a error message saying : ‘… `gets’: No such file or directory -  
12 (Errno::ENOENT)…’

Does anybody understand this ?

Thanx  
Joe

---

<div class="post-metadata">

**Author:** ![jughead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@jughead](https://www.ruby-forum.com/u/jughead)\
**Post date:** [December 29, 2005, 4:24pm UTC](https://www.ruby-forum.com/t/beginners-question/51316/2 "2005-12-29T16:24:53Z")

</div>

On Dec 29, 2005, at 9:18 AM, joe quimby wrote:

> Does anybody understand this ?  
> gets() reads from the files given as command-line arguments, or  
> STDIN, if none were given. So in your example above, “anything” is  
> expected to be the path to a file that will be read.

Just FYI, your example is also a little Perlish. Us Ruby guys  
generally write that as:

ARGF.each\_line do |line|  
puts line  
end

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jughead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@jughead](https://www.ruby-forum.com/u/jughead)\
**Post date:** [December 29, 2005, 4:28pm UTC](https://www.ruby-forum.com/t/beginners-question/51316/3 "2005-12-29T16:28:16Z")

</div>

Wow that was fast. Thanx a lot

---

<div class="post-metadata">

**Author:** ![jughead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@jughead](https://www.ruby-forum.com/u/jughead)\
**Post date:** [December 29, 2005, 4:33pm UTC](https://www.ruby-forum.com/t/beginners-question/51316/4 "2005-12-29T16:33:56Z")

</div>

Hi –

On Fri, 30 Dec 2005, joe quimby wrote:

> Wow that was fast. Thanx a lot

A quick followup: You can do:

$stdin.gets

which will force (in the case of your example) keyboard input.

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![jughead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@jughead](https://www.ruby-forum.com/u/jughead)\
**Post date:** [December 31, 2005, 1:18pm UTC](https://www.ruby-forum.com/t/beginners-question/51316/5 "2005-12-31T13:18:41Z")

</div>

On Sat, 31 Dec 2005, Daniel S. wrote:

> end  
> you could also have said:  
> #!/usr/bin/ruby  
> print while gets

> if i call ‘echo.rb’ (without any parameters) from my cygwin shell, the  
> scripts does what i expect it to do. It reads and returns the input.  
> But if i call it with any parameters, like
> 
> echo.rb anything
> 
> i get a error message saying : ‘… `gets’: No such file or directory -  
> 12 (Errno::ENOENT)…’
> 
> Does anybody understand this ?

apparently you typed:

echo.rb 12

If there are arguments, ruby interprets those as the names of files to  
be used (after concatenation) as standard input. But there was no file  
‘12’

---

<div class="post-metadata">

**Author:** ![jughead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@jughead](https://www.ruby-forum.com/u/jughead)\
**Post date:** [December 31, 2005, 12:06pm UTC](https://www.ruby-forum.com/t/beginners-question/51316/6 "2005-12-31T12:06:01Z")

</div>

joe quimby wrote:

> Thanx  
> Joe

Try this instead:

while input = $stdin.gets  
puts input  
end

Cheers,  
Daniel
