# open the file
File.open('foo.txt') do |file|
# iterate over each line with its index
file.each_line.with_index(1) do |line, number|
puts "#{number}: #{line}"
end
end
# since we passed a block, the file is automatically closed after `end`
# open the file
File.open('foo.txt') do |file|
# iterate over each line
file.each_line do |line|
puts line
end
end
# since we passed a block, the file is automatically closed after `end`
# open the file
file = File.open('foo.txt')
# iterate over each line
file.each_line do |line|
puts line
end
# close the file afterwards
file.close
File.readlines('foo').each do |line|
puts line
end
Code Example |
---|
Ruby :: rails task arguments |
Ruby :: in query postgres jsonb rails |
Ruby :: rspec parallel tests |
Ruby :: ruby delete folder recursively |
Ruby :: rspec add support folder |
Ruby :: activerecord less than |
Ruby :: create rails project with postgres |
Ruby :: max keys from hash ruby |
Ruby :: ruby check if a file exists |
Ruby :: rails remove model |
Ruby :: rails validate email |
Ruby :: remove gem rails |
Ruby :: array string ruby |
Ruby :: ruby pop array |
Ruby :: ruby array prepend vs unshift |
Ruby :: rails disable submit button |
Ruby :: read xls file in ruby |
Ruby :: rails secure uuid |
Ruby :: read headers of csv using ruby |
Ruby :: rspec gem tutorial |
Ruby :: ruby for |
Ruby :: ruby deep copy |
Ruby :: stripe test keys |
Ruby :: formatting a floating point number in ruby |
Ruby :: expect actionmailer base nullmail |
Ruby :: rails faker address |
Ruby :: my rails server exits automatically |
Ruby :: Ruby deep clone with Marshal |
Ruby :: add key and value to the beginning of a hash ruby |
Ruby :: what is ruby |