Skip to content

Instantly share code, notes, and snippets.

@masakid
Last active February 7, 2016 04:02
Show Gist options
  • Save masakid/6978dbc1addfea471afc to your computer and use it in GitHub Desktop.
Save masakid/6978dbc1addfea471afc to your computer and use it in GitHub Desktop.
部署名に上司部署名を追加していくクソコード
#! ruby -Ks
# -*- mode:ruby; coding:shift_jis
#class definition
class DepartmentData
attr_accessor :key_department_id
attr_accessor :department_name
attr_accessor :upper_department_id
def initialize(id, name, upper_id)
@key_department_id = id
@department_name = name
@upper_department_id = upper_id
end
end
# method definition
def searchUpper(hash, department_name, upper_id)
upper_data = hash[upper_id.to_sym].nil? ? "" : hash[upper_id.to_sym]
hash[upper_id.to_sym].nil? ? department_name : searchUpper(hash, "#{upper_data.department_name}/#{department_name}", upper_data.upper_department_id)
end
# procedure
# # file open
file = File.open(ARGV[0])
# file reading
text_arr = file.read.split("\n").collect!{|line| line.split(",")}
# create hash
hash_data = text_arr.inject({}){|hash, item| #create hash
hash[item[0].to_sym] = DepartmentData.new(item[0], item[1], item[2])
hash
}
# upper_department insert
text_arr.map!{ |item|
item[1] = searchUpper(hash_data, item[1], item[2])
item
}
# file output
newfile = File.open("/Users/masaki/Documents/RubyWork/result01.csv", 'w'){|f|
text_arr.each { |item|
f.write "#{item[0]}:#{item[1]}:#{item[2]}\n"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment