Module: Gren::Util

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

Constant Summary collapse

ALPHABET_DISP_NUM =

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

5
OPERATOR_DISP_NUM =
10

Class Method Summary collapse

Class Method Details

.downcase?(str) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/gren/common/util.rb', line 99

def downcase?(str)
  str == str.downcase
end

.p_classtree(c) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gren/common/util.rb', line 51

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gren/common/util.rb', line 64

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

.pipe?(io) ⇒ Boolean

StringIO patch

Returns:

  • (Boolean)


95
96
97
# File 'lib/gren/common/util.rb', line 95

def pipe?(io)
  !Platform.windows_os? && io.instance_of?(IO) && File.pipe?(io)
end

.round(n, d) ⇒ Object



24
25
26
# File 'lib/gren/common/util.rb', line 24

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

.ruby19?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/gren/common/util.rb', line 103

def ruby19?
  RUBY_VERSION >= '1.9.0'
end

.size_s(size) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gren/common/util.rb', line 28

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gren/common/util.rb', line 7

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