Ruby

help

Hi,

can anyone give a hint, why ruby skips the input, when i run this
following code?

thanks,
Markus

#!/usr/bin/ruby
def search()
puts “Suchbegriff: “;
search=gets;
chop
search = $_
search = “Welt”;
ARGV.each do |test|
File.open(”#{test}”, “r”) do |afile|
if (afile.gets =~ /#{search}/i)
puts test.to_s
end
end
end
end
search()

#!/usr/bin/ruby
def search()
    puts "Suchbegriff: ";
    search=gets;

       search = $stdin.gets # otherwise ruby read a line from the
                            # first file given in argument, and this file
                            # is removed from ARGV

    chop

Guy Decoux

“Markus Blasl” blasl@fzi.de schrieb im Newsbeitrag
news:3F2A565A.1080500@fzi.de

Hi,

can anyone give a hint, why ruby skips the input, when i run this
following code?

thanks,
Markus

#!/usr/bin/ruby
def search()
puts "Suchbegriff: ";
search=gets;

 search=$stdin.gets;
chop
search = $_
search = "Welt";
ARGV.each do |test|
    File.open("#{test}", "r") do |afile|

Don’t need the #{} here:
File.open(test, “r”) do |afile|

        if (afile.gets =~ /#{search}/i)

This reads only one line, is that desired?

            puts test.to_s
  puts test
        end
    end
end

end
search()

gets reads from ARGF, i.e. the arguments are interpreted as file names and
gets reads from them as in

cat.rb:
#!ruby
while ( line = gets ) ; puts line ; end

Regards

robert

This confused me the first time I saw it, and now I always use
$stdin.gets() instead of just gets(). Is there anyone out there who
actually uses this feature of Kernel#gets()?

Paul

···

On Fri, Aug 01, 2003 at 09:18:47PM +0900, ts wrote:

   search = $stdin.gets # otherwise ruby read a line from the
                        # first file given in argument, and this file
                        # is removed from ARGV

“Paul Brannan” pbrannan@atdesk.com schrieb im Newsbeitrag
news:20030801132057.GT13339@atdesk.com

   search = $stdin.gets # otherwise ruby read a line from the
                        # first file given in argument, and this

file

                        # is removed from ARGV

This confused me the first time I saw it, and now I always use
$stdin.gets() instead of just gets(). Is there anyone out there who
actually uses this feature of Kernel#gets()?

Yep.

robert
···

On Fri, Aug 01, 2003 at 09:18:47PM +0900, ts wrote: