From: Adrian Roskrow Date: 2007-01-18T04:53:53+09:00 Subject: Re: Reading from $stdin Robert Klemme wrote: > On 17.01.2007 14:56, Adrian Roskrow wrote: >> 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. 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? > > Do you actually redirect stdin to read from that device? > >> Below is a simple bit of code >> >> #!/usr/bin/env ruby >> text = $stdin.read >> lines = text.split("\n") >> i = 1 >> for line in lines do >> puts "#{i}. " + line >> i += 1 >> end > > I'd rather do > > $stdin.each do |line| > printf "%4d %s", $stdin.lineno, line > end > > or > > $stdin.each_with_index do |line, index| > printf "%4d %s", index, line > end > > HTH > > Kind regards > > robert Hi In answer to your question, no I don't redirct stdin, should I? I like your code snippets and will try them out post hast. Thanks Adrian -- Posted via http://www.ruby-forum.com/.