records = []
File.open('xxx.csv') do |f|
first_line = true
f.each_line do |line|
if first_line
@headers = line.chomp.split(',')
first_line = false
else
record = {}
line.chomp.split(',').each_with_index do |value, idx|
record[@headers[idx]] = value
end
records << record
end
end
end
# 白菜名
records.group_by {|record| record["Name"]}.each do |name, _records|
if @max.to_i < _records.size
@max = _records.size
@baicai_name = name
end
end
p @baicai_name, @max
# 邮箱后缀
@max = 0
records.group_by {|record| record["EMail"][/@.*$/]}.each do |email_suffix, _records|
if @max.to_i < _records.size
@max = _records.size
@baicai_email_suffix = email_suffix
end
end
p @baicai_email_suffix, @max
```ruby
你开网页有没有加代理。
#11 楼 @woody1983 你是说 extend 么
x = Thread.new { sleep 0.1; print "x"; print "y"; print "z" } a = Thread.new { print "a"; print "b"; sleep 0.2; print "c" } sleep 1
你可以先写个方法,使不同的值得到同一个 key
class Calculator [[:add, :+], [:subtract, :-], [:divide, :/], [:multiply, :], [:power, :*]].each do |m| define_method(m[0]) {|a, b| a.send(m[1], b)} end end
可以参考 Enumerable#group_by
t.gsub(/^(\d+):(\d+):(\d+)/, '\1-\2-\3')