Sha256: d60f218c553e2e76f52956c665468d4371fe6f377b7f39c496b700e6ba7f7ea7

Contents?: true

Size: 1.26 KB

Versions: 159

Compression:

Stored size: 1.26 KB

Contents

def niceprint(o, level=0, indent_first_line=true)
  maxstring = 50
  maxarray = 20
  
  result = ""
  
  dent = "    "
  indent = dent * level 
  
  result << indent if indent_first_line
  
  case o
    
    when Hash
      #puts "Hash!"
      result << "{\n"
      
      o.each_with_index do |(k,v),i|
        result << "#{indent+dent}#{k.inspect} => #{niceprint(v,level+1,false)}"
        result << "," unless i == o.size
        result << "\n"
      end
      
      result << "#{indent}}"
      
    when Array
      #puts "Array!"
      indent_first = o.any? { |e| e.instance_of? Hash }
      
      if indent_first
        result << "[\n"
      else
        result << "["
      end
      
      o = o[0..maxarray] if o.size > maxarray
      o.each do |e|
        result << niceprint(e,level+1,indent_first)
        result << ", " 
      end
      
      result << "]"
      
    when String
      #puts "String!"
      o = o[0..maxstring] + "..." if o.size > maxstring
      result << o.inspect
      
    else
      result << o.inspect
  end
  
  if level == 0
    print result
  else
    result
  end
      
end

if $0 == __FILE__
  t = {
    :a => 5,
    :b => 10,
    :c => {
      :x => 10,
      :y => [1,2,3,4,5,6,7],
    },
    :d => "asdf"*1000,
  }
  
  puts niceprint(t)
end

Version data entries

159 entries across 159 versions & 1 rubygems

Version Path
epitools-0.5.103 lib/epitools/niceprint.rb
epitools-0.5.100 lib/epitools/niceprint.rb
epitools-0.5.99 lib/epitools/niceprint.rb
epitools-0.5.98 lib/epitools/niceprint.rb
epitools-0.5.97 lib/epitools/niceprint.rb
epitools-0.5.96 lib/epitools/niceprint.rb
epitools-0.5.95 lib/epitools/niceprint.rb
epitools-0.5.94 lib/epitools/niceprint.rb
epitools-0.5.93 lib/epitools/niceprint.rb
epitools-0.5.92 lib/epitools/niceprint.rb
epitools-0.5.91 lib/epitools/niceprint.rb
epitools-0.5.90 lib/epitools/niceprint.rb
epitools-0.5.89 lib/epitools/niceprint.rb
epitools-0.5.88 lib/epitools/niceprint.rb
epitools-0.5.87 lib/epitools/niceprint.rb
epitools-0.5.86 lib/epitools/niceprint.rb
epitools-0.5.85 lib/epitools/niceprint.rb
epitools-0.5.84 lib/epitools/niceprint.rb
epitools-0.5.83 lib/epitools/niceprint.rb
epitools-0.5.82 lib/epitools/niceprint.rb