Ruby/File Directory/IO.foreach

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Print all lines containing the word "target"

IO.foreach("somefile") do |line|
  puts line if line =~ /target/
end
# Another way...
file = File.new("somefile")
file.each do |line|
  puts line if line =~ /target/
end



Read lines one at a time and initialize a hash

words = {}
IO.foreach("/usr/share/dict/words") {|w| words[w] = true}