From: gotoken@... (GOTO Kentaro) Date: 1998-12-19T06:44:51+09:00 Subject: [ruby-talk:00123] Re: finished simple script In message "[ruby-talk:00122] Re: finished simple script" on 98/12/18, Julian Fondren writes: >> Humm... I can't find such situation for the above script :-( >> Would you tell me what input shows that ``.'' is translated to 32, >> or explain me the reason why you think so? > >I don't know. Yesterday 1 * . gave 32.. but then yesterday this script >also allowed me to press enter 2000 times, 1, enter 2000 times, 2 and >get the answer of 2. Strange. To consider the problem, I tried as follows. I put ``p [ans1, ans2]'' at the end of your script, say `get2nums.rb', then the script was as follows: (`%' for the shell prompt) % cat get2nums.rb #!/usr/bin/env/ruby print "Input two numbers to multiply together: \n" ans1, ans2 = STDIN.gets.chomp.scan(/\d+/).collect { |x| x.to_i } while !ans1 if !ans1 ans1, ans2 = STDIN.gets.to_i, STDIN.gets.to_i end end while !ans2 if !ans2 ans2 = STDIN.gets.to_i end end p [ans1,ans2] % With the above, I got: % ruby -v ruby 1.1c9(98/11/26) [i386-freebsd2.1.0] % echo "1 * ." | ruby get2nums.rb Input two numbers to multiply together: [1, 0] % ruby -e 'print ("\n"*2000,1,"\n"*2000,2)' | ruby get2nums.rb Input two numbers to multiply together: [0, 0] % ... Maybe, I can't get as same as what you get :-( Would you try the aboves and show me the results? However, I see because of what I got. The pieces of the fact are "1 * .\n".chomp #=> "1 * ." "1 * .".scan(/\d+/) #=> ["1"] ["1"].collect{|x| x.to_i} #=> [1] ans1, ans2 = [1] #=> ans1 == 1, ans2 == nil nil.to_i #=> 0 "\n".to_i #=> 0 etc... By the way, `p' is a built-in function which is mimicked by def p(*args) STDOUT.print *args.collect{|elm| elm.inspect + "\n"} STDOUT.flush nil end This function is often useful for quick debugging. I hope that you know `p' already. -- gotoken