Counting Characters : Read lines together « File Directory « Ruby






Counting Characters



lines = File.readlines("text.txt")
line_count = lines.size
text = lines.join

puts "#{line_count} lines"

total_characters = text.length
puts "#{total_characters} characters"

 








Related examples in the same category

1.File.readlines
2.You can also read an entire file into an array, split by lines