Class: Array

Inherits:
Object show all
Defined in:
lib/pretty_debug.rb,
lib/pretty_debug.rb

Overview

String Formatting

Direct Known Subclasses

PrettyArray

Constant Summary collapse

SingleLength =
50

Instance Method Summary collapse

Instance Method Details

#align(ellipsis_limit = nil) ⇒ Object



284
285
286
287
288
289
290
291
# File 'lib/pretty_debug.rb', line 284

def align ellipsis_limit = nil
  transpose.map do |col|
    just = case col.first; when Numeric then :rjust; else :ljust end
    width = col.map{|cell| cell.to_s.length}.max
    max = ellipsis_limit || width
    col.map{|cell| cell.to_s.ellipsis(max).__send__(just, width > max ? max : width)}
  end.transpose
end

#common_affixObject

first[0, (0…first.length).find{|i| first.! or others.any?{|s| first != s}}.to_i]



301
302
303
304
305
306
307
# File 'lib/pretty_debug.rb', line 301

def common_affix
  each{|e| class_check(String, e){"All elements must be a String".freeze}}
  first, *others = self
  i = - 1
  loop{break unless first[i] and others.all?{|s| first[i] == s[i]}; i -= 1}
  first[i + 1, -(i + 1)]
end

#common_prefixObject



292
293
294
295
296
297
298
299
300
# File 'lib/pretty_debug.rb', line 292

def common_prefix
  each{|e| class_check(String, e){"All elements must be a String".freeze}}
  first, *others = self
  i = 0
  loop{break unless first[i] and others.all?{|s| first[i] == s[i]}; i += 1}
  first[0, i]
# This is more elegant, but cannot be generalized to `common_affix`.
##    first[0, (0...first.length).find{|i| first[i].! or others.any?{|s| first[i] != s[i]}}.to_i]
end

#inspectObject



230
231
232
233
234
235
# File 'lib/pretty_debug.rb', line 230

def inspect
  return original_inspect if recursive?
  s = map(&:inspect)
  length < 2 || s.map(&:length).inject(:+) < SingleLength ?
    "[#{s.join(", ".freeze)}]" : "[#$/#{s.join(",#$/".freeze).indent}#$/]"
end

#original_inspectObject



229
# File 'lib/pretty_debug.rb', line 229

alias original_inspect inspect