[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

stubborn program using readline

Wybo Dekker

6/16/2005 10:38:00 AM

9 Answers

Logan Capaldo

6/16/2005 10:52:00 AM

0

On 6/16/05, Wybo Dekker <wybo@servalys.nl> wrote:
> Hi,
>
> I've been on the list a few days ago with a readline-problem, for which
> there doesn't seem to be a quick fix. I'm now trying a dirty fix... but
> this seems to be a very stubborn problem.
>
> Here is my original test program
>
> # rltest - test for readline
> # run me like this: (i.e. operating on a file)
> #
> # ruby rltest rltest
> #
> # and all is fine. But run me like so: (i.e. operating on stdin)
> #
> # ruby rltest < rltest
> #
> # and see that I successfully count my own lines,
> # then also print the "type return to quit" prompt,
> # but finally seem to be looping in the readline function, and
> # can only be stopped with ctrl-C,
> # ( not even with ctrl-D - I'm not looking at the terminal)
>
> require 'readline'
> include Readline
>
> # count lines in stdin (the mail message):
> n=0; while gets: n+=1 end
> puts "#{n} lines"
>
> readline("type return to quit: ")
> puts "quitting..."
>
> I tried to get around this problem by testing if input is from stdin,
> and if so, making a temporary copy of stdin and re-running the program
> on that copied file:
>
> file = ARGV.shift || ''
> if file == ''
> require 'tempfile'
> f = Tempfile.new('vpp')
> name = f.path
> puts "Making temporary file: #{name}"
> n = 0
> while gets
> f.puts
> n += 1
> end
> f.close
> puts "Wrote #{n} lines"
> system($0, name)
> else
> require 'readline'
> include Readline
> puts "reading #{file}"
> # count lines in stdin (the mail message):
> n = 0
> open(file).each do
> n+=1
> end
> puts "Saw #{n} lines"
>
> readline("type return to quit: ")
> puts "quitting..."
> end
>
> Running this with: ruby rltest rltest says:
>
> reading rltest
> Saw 30 lines
> type return to quit:
> quitting...
>
> But to my astonishment, running with: ruby rltest < rltest stays in a
> loop, like the original test program:
>
> Making temporary file: /data/tmp/vpp19009.0
> Wrote 30 lines
> reading /data/tmp/vpp19009.0
> Saw 30 lines
> type return to quit:
> ./rltest:28:in `readline': Interrupt
> from ./rltest:28
>
> Can anybody explain this?
>
> --
> Wybo
>
>

Theory: readline reads from STDIN correct? using rltest < rltest means
that STDIN gets EOF, which means STDIN is closed which means readlien
can't read anything from it. I don't think you can have your cake and
eat it too in this case. If you want the interactive prompt your going
to have pass the file name on the command line instead of using IO
redirection. I'm a bit confused as to the purpose of this.


Wybo Dekker

6/16/2005 10:41:00 PM

0

Jonathan Paisley

6/17/2005 12:08:00 PM

0

On Fri, 17 Jun 2005 08:41:22 +0900, Wybo Dekker wrote:

> On Thu, 16 Jun 2005, Logan Capaldo wrote:
>
>> Theory: readline reads from STDIN correct? using rltest < rltest means
>> that STDIN gets EOF, which means STDIN is closed which means readlien
>> can't read anything from it.
>
> No, readline should not necessarily read from STDIN. It can read from the
> console, /dev/tty or so.

Since you always want to readline from the terminal, try that:

tty = open("/dev/tty","a+")
tty.readline


Wybo Dekker

6/17/2005 2:29:00 PM

0

James Gray

6/17/2005 3:00:00 PM

0

On Jun 17, 2005, at 9:28 AM, Wybo Dekker wrote:

> What I need is a translation of this simple Perl example into Ruby -
> I think Ruby can't do this:

[snip code]

I think it can:

#!/usr/local/bin/ruby -w

require "readline"
include Readline

lines = ARGF.readlines
puts "#{lines.size} lines."

loop do
line = readline("Line number: ", true)

if line !~ /^\d+$/
puts "Please enter a positive integer."
else
number = line.to_i
case number
when 0
break
when 1..lines.size
puts "Line #{number}: #{lines[number - 1]}"
else
puts "No such line."
end
end
end

__END__

My run:

$ ruby showlines.rb showlines.rb
25 lines.
Line number: 1
Line 1: #!/usr/local/bin/ruby -w
Line number: 16
Line 16: case number
Line number: 17
Line 17: when 0
Line number: 18
Line 18: break
Line number: 1003
No such line.
Line number: darn
Please enter a positive integer.
Line number: 0

Hope that helps.

James Edward Gray II


Wybo Dekker

6/17/2005 3:40:00 PM

0

Ara.T.Howard

6/17/2005 4:18:00 PM

0

Wybo Dekker

6/17/2005 8:38:00 PM

0

Ara.T.Howard

6/17/2005 9:26:00 PM

0