From: ara.t.howard@... Date: 2007-01-18T00:26:17+09:00 Subject: Re: Reading from $stdin On Wed, 17 Jan 2007, Adrian Roskrow wrote: > Hi > > I need to read from a usb connected barcode reader and I thought ruby > would be a good way to do it. I have written a quick program to read > from the $stdin object but it just sits there and waits. but this is __exactly__ what you told it to do? harp:~ > PAGER=cat ri 'IO#read' ---------------------------------------------------------------- IO#read ios.read([length [, buffer]]) => string, buffer, or nil ------------------------------------------------------------------------ Reads at most _length_ bytes from the I/O stream, or to the end of file if _length_ is omitted or is +nil+. _length_ must be a non-negative integer or nil. If the optional _buffer_ argument is present, it must reference a String, which will receive the data. At end of file, it returns +nil+ or +""+ depend on _length_. +_ios_.read()+ and +_ios_.read(nil)+ returns +""+. +_ios_.read(_positive-integer_)+ returns nil. f = File.new("testfile") f.read(16) #=> "This is line one" so, in this case, you are reading until the end of stdin. you can enter eof on the command line (in *nix) with ctrl-d. > Im very new to ruby so I have probably made a fundamental mistake. Can > anyone see what I have done, or can anyone point me in the right direction > or provide an example? afaikt your code works perfectly: harp:~ > cat a.rb #!/usr/bin/env ruby text = $stdin.read lines = text.split("\n") i = 1 for line in lines do puts "#{i}. " + line i += 1 end harp:~ > printf "42\n43\n" | ruby a.rb 1. 42 2. 43 in any case it looks like you're on the right track - welcome to ruby! -a -- in the practice of tolerance, one's enemy is the best teacher. - the dalai lama