Sha256: bb62ee79c821420ba0aec2d2678dcf8633beb5372eaa85a4ec32045a0612b988

Contents?: true

Size: 1.54 KB

Versions: 76

Compression:

Stored size: 1.54 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

76 entries across 76 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.116 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.115 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.114 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.113 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.112 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.111 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.110 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.109 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.108 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.107 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.106 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.105 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.104 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.103 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.102 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.101 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.100 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.99 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.98 lib/open_classes/kernel/aa_ancestors.rb
tbpgr_utils-0.0.97 lib/open_classes/kernel/aa_ancestors.rb