Module: Gren::Util

Defined in:
lib/milkode/common/util.rb

Constant Summary collapse

ALPHABET_DISP_NUM =

アルファベットと演算子で表示する数を変える

5
OPERATOR_DISP_NUM =
10

Class Method Summary collapse

Class Method Details

.p_classtree(c) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/milkode/common/util.rb', line 387

def p_classtree(c)
  unless c.is_a?(Class)
    c = c.class
  end
  
  while (true)
    puts c.name
    break if (c == Object)
    p_classtree_sub(c)
    c = c.superclass
  end
end

.p_classtree_sub(c) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/milkode/common/util.rb', line 401

def p_classtree_sub(c)
  # メソッドの一覧を得る
  group = c.public_instance_methods(false).sort.partition { |m| m =~ /\w/ }
  array = group.flatten
  operator_start_index = group[0].size
  limit = ALPHABET_DISP_NUM

  print((array.size > limit) ? "" :  "")
  
  counter = 0
  array.each_with_index do |v, index|
    if (index == operator_start_index)
      limit = OPERATOR_DISP_NUM
      counter = 0
      puts
      print((array.size - index > limit) ? "" : "")
    end

    if (counter >= limit)
      counter = 0
      puts
      print((array.size - index > limit) ? "" : "")
    end

    print v + ", "
    counter += 1
  end
  puts
end

.round(n, d) ⇒ Object



362
363
364
# File 'lib/milkode/common/util.rb', line 362

def round(n, d)
  (n * 10 ** d).round / 10.0 ** d
end

.size_s(size) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/milkode/common/util.rb', line 367

def size_s(size)
  tb = 1024 ** 4
  gb = 1024 ** 3
  mb = 1024 ** 2
  kb = 1024

  if (size >= tb)
    round(size / tb.to_f, 2).to_s + "TB"
  elsif (size >= gb)
    round(size / gb.to_f, 2).to_s + "GB"
  elsif (size >= mb)
    round(size / mb.to_f, 2).to_s + "MB"
  elsif (size >= kb)
    round(size / kb.to_f, 2).to_s + "KB"
  else
    size.to_s + "Byte"
  end
end

.time_s(time) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/milkode/common/util.rb', line 344

def time_s(time)
  t = time.truncate
  h = t / 3600
  t = t % 3600
  m = t / 60
  t = t % 60
  t += round(time - time.to_i, 2)
  
  if (h > 0 && m > 0)
    "#{h}h #{m}m #{t}s"
  elsif (m > 0)
    "#{m}m #{t}s"
  else
    "#{t}sec"
  end
end