Ruby Tidbit: __LINE__ and heredocs

You’re probably familiar with __LINE__ keyword in Ruby. Wherever it’s used, it refers to the line number in the current file. It says so right here in the Ruby docs.

QUICK! What’s the output of the following code?

CODE:

#!/usr/bin/env ruby

class LineDemo
  def self.process(text, lineno)
    # do important processing on text
    puts lineno
  end
end

LineDemo.process < Here is some text on which we will do some important processing.
Here, also, is some important text to process.
TEXT
# All done!The answer is obvious. Of course, it prints 13.



请使用浏览器的分享功能分享到微信等