Нашли или выдавили из себя код, который нельзя назвать нормальным,
на который без улыбки не взглянешь?
Не торопитесь его удалять или рефакторить, — запостите его на
говнокод.ру, посмеёмся вместе!
columns.each do |c|
case c
when 'Device'
h << c
when 'Usage'
h << c
when 'Status'
h << c
when 'Battery'
h << c
when 'GPS status'
h << c
when 'Wi-Fi status'
h << c
when 'Temperature'
h << c
when 'Alerts'
h << c
end
end
#!/usr/bin/ruby1.8
require 'mysql'
$KCODE = 'UTF8'
class Country
@@country = Array.new
@@insert_query = String.new
@@db = Mysql
def initialize(filename)
file = File.open(filename)
while !file.eof?
value, index = file.readline.split(/\s+/u)
@@country[index.to_i] = value.to_s
end
file.close
end
def database_connect
@@db = Mysql.new('localhost','username','userpass','userdatabase')
begin
@@db.query("SET NAMES utf8")
rescue
puts @@db.error
end
end
def create_query
begin
result = @@db.query("SELECT * FROM table")
rescue
puts @@db.error
end
result.each_hash do |field|
@@country.each_index do |index|
@@insert_query += "UPDATE table SET position = #{index} WHERE caption = '#{field['caption']}';" if @@country[index] == field['caption']
end
end
end
def execute_query
begin
@@insert_query.split(/;/u).each { |query| @@db.query(query) }
puts "result: #{@@db.errno}" if @@db.errno
rescue
puts @@db.error
end
end
def database_disconnect
@@db.close
end
end
cnt = Country.new('country.txt')
# connect to DB
cnt.database_connect
# construct query
cnt.create_query
# execute constructed query
cnt.execute_query
# close connect
cnt.database_disconnect
def get_line_from_file(path, line)
result = nil
File.open(path, "r") do |f|
while line > 0
line -= 1
result = f.gets
end
end
return result
end
get_line_from_file("/tmp/foo.txt", 20)
i = -1
ans = threes.map do |s|
i+= 1
n = s.to_i
next if n == 0
if i == 0
nums[n]
elsif i == 1
# тысячи
e = n % 10
d = n % 100
suff = ok_z[e]
suff = ok_z[0] if d > 10 and d < 20
t = nums[n]
if e == 1 or e == 2 and not (d > 10 and d < 20)
# один и два перед тысячами меняются.
# не регексп, чтобы работал и юникод и однобайтные кодировки
t = " " + t + " "
t = t.sub(" один "," одна ").sub(" два "," две ")
t = t[1..99].chop
end
t + (n!=0 ? " " : "") + threes_names[i-1] + suff
else
e = n % 10
d = n % 100
suff = ok_m[e]
suff = ok_m[0] if d > 10 and d < 20
nums[n] + (n!=0 ? " " : "") + threes_names[i-1] + suff
end
end.reverse.compact.join(" ")
puts ans