Mercurial > repos > other > SevenLanguagesInSevenWeeks
view 1-Ruby/day2-grep.rb @ 103:98be775c533c default tip
An odd "non-determinism" example from StackOverflow
It is clever, but doesn't make much sense as to how it gets its results
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 14 Jul 2019 13:44:13 +0100 |
parents | 720e9201dd98 |
children |
line wrap: on
line source
#! /usr/bin/env ruby if ARGV.length != 2 abort("Incorrect arguments - pass a single file and a pattern") elsif not File.exist? (ARGV[0]) abort ("File #{ARGV[0]} did not exist") end file = ARGV[0] pattern = Regexp.new(ARGV[1]) i = 0 File.open(file, "r").each_line do |line| i+= 1 if line =~ pattern then puts "#{i} #{line}" end end