返回顶部

收藏

Picking a Random Line from a File

更多
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'
f.random_line                                    # => nil
f.rewind
f.random_line                                    # => 'Line 727'

标签:Ruby,文件

收藏

0人收藏

支持

0

反对

0

发表评论