Ruby UnRails - sgml/signature GitHub Wiki

module SidewaysTree
  def self.generate(roots, branches, indentation = 10)
    output = ""

    roots.each_with_index do |root, idx|
      output << "#{' ' * indentation}+#{'-' * (root.length + 4)}+\n"
      output << "#{' ' * indentation}|  #{root}  |\n"
      output << "#{' ' * indentation}+#{'-' * (root.length + 4)}+\n"

      if branches[idx]
        build_branches(branches[idx], indentation * 2, output)
      end

      output << "\n\n"
    end

    output
  end

  def self.build_branches(branch_list, indent, output)
    branch_list.each do |branch|
      if branch.is_a?(Array)
        build_branches(branch, indent + 4, output)
      else
        output << "#{' ' * indent}|=>> #{branch}\n"
      end
    end
  end
end

# -------------------------------
# Sample Input (as heredoc)
# -------------------------------
sample_input = <<~RUBY
  roots = ["SOFTWARE ENGINEERING", "INFRASTRUCTURE"]
  branches = [
    ["Knowledge Base Curation",
     "Fraud Detection Automation",
     "Audit Dashboard",
     ["Data", ["Metadata Classification",
               ["Data Deduplication",
                ["Data Enrichment",
                 ["Member Segmentation"]]]]]],
    ["Python", "DevOps", "Front-End Frameworks",
     "Code Review", "Testing", "Data Security", "Data Pipeline"]
  ]

  puts SidewaysTree.generate(roots, branches, 10)
RUBY

# -------------------------------
# Sample Output (as heredoc)
# -------------------------------
sample_output = <<~TEXT
          +----------------------------+
          |  SOFTWARE ENGINEERING      |
          +----------------------------+
                    |=>> Knowledge Base Curation
                    |=>> Fraud Detection Automation
                    |=>> Audit Dashboard
                    |=>> Data
                          |=>> Metadata Classification
                                |=>> Data Deduplication
                                      |=>> Data Enrichment
                                            |=>> Member Segmentation


          +------------------+
          |  INFRASTRUCTURE  |
          +------------------+
                    |=>> Python
                    |=>> DevOps
                    |=>> Front-End Frameworks
                    |=>> Code Review
                    |=>> Testing
                    |=>> Data Security
                    |=>> Data Pipeline
TEXT

# Print sample input and output
puts "Sample Input:\n#{sample_input}"
puts "Sample Output:\n#{sample_output}"