The following code adds a variable, text, to collect the lines together:
text='' line_count = 0 # w w w . j a v a2s .co m File.open("main.rb").each do |line| line_count += 1 text << line end puts "#{line_count} lines"