Guest User

Untitled

a guest
Feb 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. # original slow method to find a number plus a divider (|)
  2. # at the start of a line
  3. def find_num(file, num)
  4. found = false
  5. File.read(file).each do |line|
  6. if line.chomp.split('|', -1)[0] == num
  7. found = true
  8. break
  9. end
  10. end
  11.  
  12. # new method that uses the system's grep
  13. # since this calls the system, make sure that you trust the values
  14. # of file and num
  15. def find_num(file, num)
  16. found = false
  17. found = true if %x[grep -cm1 "^#{num}|" "#{file}"].chomp == '1'
  18. found
  19. end
Add Comment
Please, Sign In to add comment