Module: Utils

Defined in:
lib/kaki/utils.rb

Class Method Summary collapse

Class Method Details

.bellObject

ベルを鳴らす(Ubuntuのみ)



93
94
95
# File 'lib/kaki/utils.rb', line 93

def bell
  `paplay /usr/share/sounds/freedesktop/stereo/complete.oga`
end

.combination(a, b) ⇒ Integer

aの中からb個選ぶ組み合わせの数



117
118
119
# File 'lib/kaki/utils.rb', line 117

def combination(a, b)
  Utils.permutation(a, b) / Utils.factorial(b)
end

.delete_empty_folder(fname = './') ⇒ Object

指定したディレクトリの空フォルダを削除



82
83
84
85
86
87
88
# File 'lib/kaki/utils.rb', line 82

def delete_empty_folder(fname = './')
  Dir.chdir(fname)
  Dir.glob("*").each do |name|
    next unless File.directory?(name)
    Dir.rmdir(p name) if Dir.glob("./#{name}/*").empty? rescue next
  end
end

.delete_non_img(fname) ⇒ Boolean

画像ファイルでなければ削除



70
71
72
73
74
75
76
77
# File 'lib/kaki/utils.rb', line 70

def delete_non_img(fname)
  if Utils.imgexist?(fname)
    false
  else
    FileUtils.rm(fname)
    true
  end
end

.factorial(n) ⇒ Integer

階乗



101
102
103
104
105
# File 'lib/kaki/utils.rb', line 101

def factorial(n)
  result = 1
  2.upto(n) {|i| result *= i}
  result
end

.generate_random_strings(num, string_length = nil) ⇒ Array

アルファベット小文字を使った長さ string_length の文字列を、重複しないようにランダムに num 個生成する。string_length が指定されないときは最短の文字列を使う



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/kaki/utils.rb', line 137

def generate_random_strings(num, string_length = nil)
  table = [*"a".."z"]
  limit = [0, 26, 702, 18278, 475254, 12356630]
  result = []
  generate_string1 = ->(n, l) {
    st = ""
    l.times do
      a, n = n % 26, n / 26
      st = table[a] + st
    end
    st
  }
  generate_string2 = ->(n) {
    idx = limit.find_index {|i| i > n}
    generate_string1.(n - limit[idx - 1], idx)
  }
  
  if string_length and 26 < string_length
    raise "Given length of strings too big."
  end
  
  num_table = Set.new
  if string_length
    n = Utils.repeated_permutation(26, string_length)
    raise "Given length of strings too small." if n < num
    while num_table.size < num
      num_table << rand(n)
    end
    num_table.each {|i| result << generate_string1.(i, string_length)}
  else
    idx = limit.find_index {|i| i >= num}
    raise "Result Array too big." unless idx
    while num_table.size < num
      num_table << rand(limit[idx])
    end
    num_table.each {|i| result << generate_string2.(i)}
  end
  result
end

.getfile(url, filename, max = 0) ⇒ Boolean

Web上のバイナリファイルの保存



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kaki/utils.rb', line 17

def getfile(url, filename, max=0)
  count = 0
  begin
    open(filename, 'wb') do |file|
      open(url) {|data| file.write(data.read)}
    end
    true 
  rescue
    puts "ファイル入出力エラー: " + $!.message.encode("UTF-8")
    count += 1
    return false if count > max  #max回までリトライする
    puts "リトライ: #{count}"
    sleep(1)
    retry
  end
end

.imgexist?(url) ⇒ Boolean

画像か?



10
11
12
# File 'lib/kaki/utils.rb', line 10

def imgexist?(url)
  FastImage.size(url)
end

.key_waitString

一文字入力



38
39
40
41
42
43
# File 'lib/kaki/utils.rb', line 38

def key_wait
  c = nil
  loop {break if (c = STDIN.getch)}
  STDIN.cooked!
  c
end

.permutation(a, b) ⇒ Integer

a個の中からb個選ぶ順列の数



111
112
113
# File 'lib/kaki/utils.rb', line 111

def permutation(a, b)
  [*1..a].last(b).inject(1, &:*)
end

.progress_barObject

プログレスバーの表示



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kaki/utils.rb', line 48

def progress_bar
  w = Gtk::Window.new
  w.signal_connect("destroy") {Gtk.main_quit}
  w.set_size_request(300, 50)
  w.border_width = 10
  w.title = "Progress"
  
  bar = Gtk::ProgressBar.new
  w.add(bar)

  Thread.new(bar) do |bar|
    yield(bar)
  end
    
  w.show_all
  Gtk.main
end

.repeated_combination(a, b) ⇒ Integer

a個の中から重複を許してb個選ぶ組み合わせの数



129
130
131
# File 'lib/kaki/utils.rb', line 129

def repeated_combination(a, b)
  Utils.combination(a + b - 1, b)
end

.repeated_permutation(a, b) ⇒ Integer

a個の中から重複を許してb個選ぶ順列の数



123
124
125
# File 'lib/kaki/utils.rb', line 123

def repeated_permutation(a, b)
  a ** b
end