Sha256: 05d6ef3fbf013ae1c12fa59abf2a30fccf14964006f7165612db59e3dd1b8b75

Contents?: true

Size: 1.51 KB

Versions: 34

Compression:

Stored size: 1.51 KB

Contents

# encoding: utf-8
require 'open_classes/string'

# Kernel
module Kernel
  # Ascii Art Ancestors
  #
  # === Examples
  #
  # define class
  #
  #   class BaseHogeForAncestors;end
  #   class HogeForAncestors < BaseHogeForAncestors;end
  #
  # execute aa_ancestors
  #
  #   puts HogeForAncestors.aa_ancestors
  #
  # result is ...
  #
  #   ----------------------
  #   |     BasicObject    |
  #   ----------------------
  #             |
  #   ----------------------
  #   |       Kernel       |
  #   ----------------------
  #             |
  #   ----------------------
  #   |       Object       |
  #   ----------------------
  #             |
  #   ----------------------
  #   |BaseHogeForAncestors|
  #   ----------------------
  #             |
  #   ----------------------
  #   |  HogeForAncestors  |
  #   ----------------------
  def aa_ancestors
    max_class = max_size_class
    max = max_class.to_s.size
    ca = centered_ancestors(max)
    ba = surrounded_ancestors(ca)
    connected_box_ancestors(ba, max)
  end

  private

  def max_size_class
    ancestors.max_by { |v|v.to_s.size }
  end

  def centered_ancestors(max)
    ancestors.reverse.map { |v|v.to_s.center max }
  end

  def surrounded_ancestors(ca)
    ca.reduce([]) { |ret, k|ret << k.surround }
  end

  def connected_box_ancestors(ba, max)
    ba.join(ancestors_connector(max))
  end

  def ancestors_connector(max)
    connector = '|'.rjust((max + 2) / 2)
    "\n#{connector}\n"
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.150 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.149 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.148 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.147 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.146 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.145 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.144 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.143 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.142 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.141 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.140 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.139 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.138 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.137 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.136 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.135 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.134 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.133 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.132 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.131 lib/open_classes/kernel/aa_ancestors.rb