Today, i wanted to write to write a program that would open a file and display line by line with numbers (like nl in linux). And with Ruby, i did it within 2 minutes( i am not joking but not surprised anyways.) I took just a one liner.
count = 0
File.open(“foo.txt”,”r”).each {|i| puts “#{count + = 1} #{i.chomp}”}
Extended Version.
count = 0
File.open(“foo.txt”,”r”).each do |i|
puts “#{count += 1} #{i.chomp}”
end

No comments yet
Comments feed for this article