Class: Util

Inherits:
Object
  • Object
show all
Defined in:
lib/geocoder-simplified/util.rb

Constant Summary collapse

MAX_INT32 =
0x7fffffff
SECONDS_PER_DAY =
4 * 60 * 60

Class Method Summary collapse

Class Method Details

.capture_stderrObject



37
38
39
40
41
42
43
# File 'lib/geocoder-simplified/util.rb', line 37

def capture_stderr
  previous_stderr, $stderr = $stderr, StringIO.new
  yield
  $stderr.string
ensure
  $stderr = previous_stderr
end

.dump_database(db) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/geocoder-simplified/util.rb', line 18

def dump_database(db)
  puts "-" * 76
  puts "database dump:"
  keys = db.keys
  if keys.size > 0
    values = db.mget(*keys)
    list = Hash[keys.zip(values)]
    list.each{|k, v| puts "%s = %s" % [k, v]}
  end
end

.float_with_commas(n, decimal_places) ⇒ Object



28
29
30
31
32
33
# File 'lib/geocoder-simplified/util.rb', line 28

def float_with_commas(n, decimal_places)
  whole = Integer(n)
  fraction = n - whole
  decimal_format = "%%.%df" % decimal_places
  whole.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse << (decimal_format % fraction)[1..-1]
end

.get_16bit_hash_id(s) ⇒ Object



34
35
36
# File 'lib/geocoder-simplified/util.rb', line 34

def get_16bit_hash_id(s)
  "%04x" % s.bytes.each_with_index.map{|c,i| c << ((i & 1) * 8)}.inject(0xFFFF){|hash, n| hash ^ n}
end