Module: Ace::Bundle::Atoms::LineCounter

Defined in:
lib/ace/bundle/atoms/line_counter.rb

Overview

Pure function to count lines in content Follows ATOM architecture: no side effects, single purpose

Class Method Summary collapse

Class Method Details

.count(content) ⇒ Integer

Count the number of lines in content

Examples:

Empty content

LineCounter.count("")  # => 0

Single line

LineCounter.count("hello")  # => 1

Multiple lines

LineCounter.count("a\nb\nc")  # => 3

Trailing newline (does not add extra line)

LineCounter.count("a\nb\n")  # => 2

Parameters:

  • content (String, nil)

    Content to count lines in

Returns:

  • (Integer)

    Number of lines (0 for empty/nil content)



25
26
27
28
29
30
31
32
# File 'lib/ace/bundle/atoms/line_counter.rb', line 25

def count(content)
  return 0 if content.nil? || content.empty?

  # Count actual lines of content
  # "a\nb\nc" => 3 lines
  # "a\nb\n" => 2 lines (trailing newline doesn't add a line)
  content.count("\n") + (content.end_with?("\n") ? 0 : 1)
end