Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ys1/parent_and_child.rb

Overview

OpenClass String

Instance Method Summary collapse

Instance Method Details

#to_pacs(start_line) ⇒ Array<YS1::ParentAndChild>

Note:

For very large data, consider splitting the file using the csplit command before processing to avoid loading the entire file into memory at once.

Converts the string into an array of YS1::ParentAndChild objects.

Each line that matches the start_line pattern becomes a new parent object. Subsequent lines are added as children to the most recent parent object.

Examples:

Basic usage

content = <<~DATA
  Parent 1
  Child 1
  Child 2
  Parent 2
  Child 3
DATA

parents = content.to_pacs(/^Parent/)
parents.each do |parent|
  puts parent.line
  parent.children.each { |child| puts "  #{child}" }
end

Parameters:

  • start_line (Regexp)

    The regular expression to identify the start of a parent block.

Returns:



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ys1/parent_and_child.rb', line 72

def to_pacs(start_line)
  each_line.with_object([]) do |raw_line, pacs|
    line = raw_line.chomp

    if line.match?(start_line)
      pacs << YS1::ParentAndChild.new(line)
    else
      pacs.last&.add_child(line)
    end
  end
end