 
 This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
| Picking a Random Line from a File | ||
|---|---|---|
| Code | Expected | Actual | 
| module Enumerable
  def random_line
    selected = nil
    each_with_index { |line, lineno| selected = line if rand < 1.0/lineno }    
    return selected.chomp if selected
  end
end
#Create a file with 1000 lines
open('random_line_test', 'w') do |f| 
  1000.times { |i| f.puts "Line #{i}" }
end
#Pick random lines from the file.
f = open('random_line_test')
f.random_line | "Line 520" | "Line 520" | 
| f.random_line | nil | nil | 
| f.rewind f.random_line | "Line 727" | "Line 727" | 
| File.open('random_line_test') do |f|
  l = f.readlines
  l[rand(l.size)].chomp
end | "Line 708" | "Line 708" |