[ next ] [ prev ] [ contents ] [ skip to Understanding Blocks ] Using Ruby

Ruby Immersion

Count the number of lines in a file

counter = 0
while line = gets
  counter += 1
end
puts "Number of lines = #{counter}"

  • Immediate Execution
  • String Interpolation

Output

Number of lines = 5


[ next ] [ prev ] [ contents ] [ skip to Understanding Blocks ] Copyright 2003 by Jim Weirich.
Some Rights Reserved