Your code looks pretty good to me, only that it's very imperative. I know it's not for everybody's taste, but a functional approach on text processing is also feasible.
My take: you start by defining an object (which may be a hash) to store the state (needed for in intermediate iterations and the final result), and then fold (reduce/inject) over the input lines, returning the next state on each step.
There are some edge cases (lines with multiple or nested comment) which I won't consider, let's keep it simple. I'd write:
initial_state = {lines: 0, comments: 0, in_multiline_comment: false}
final_state = open("test.txt").lines.inject(initial_state) do |state, line|
state_update = if state[:in_multiline_comment]
{comments: state[:comments] + 1, in_multiline_comment: !line.match(/\*\//)}
else
case line
when /\/\*/
{comments: state[:comments] + 1, in_multiline_comment: !line.match(/\*\//)}
when /\/\//
{comments: state[:comments] + 1}
else
{lines: state[:lines] + (line.strip.empty? ? 0 : 1)}
end
end
state.merge(state_update)
end
Notice that you don't worry about stateful variables anymore, we just think "if I have this input and this current state, what output would I want?". Once you've covered all scenarios, the algorithm is done (hopefully leaving behind a declarative and maintainable code).