Last active
January 13, 2016 03:14
-
-
Save cloudy9101/07d08570b522443f9f10 to your computer and use it in GitHub Desktop.
lucky_draw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'date' | |
current_path = File.expand_path(File.dirname(__FILE__)) | |
# 获得参与抽奖的用户 users | |
def get_users file | |
users = [] | |
File.open(file) do |f| | |
f.readlines.each do |line| | |
users << line.sub(/\n/, '').split(',') | |
end | |
end | |
return users | |
end | |
# 抽奖: shuffle 方法对 users 数组随机排序,取前 n 个用户 | |
lucky_users = (get_users ARGV[0]).shuffle.first(ARGV[1].to_i) | |
# 将得奖用户 lucky_users 的 id 存入文件 | |
File.open("#{current_path}/#{Time.now}.txt", 'w') do |f| | |
f.write(lucky_users.map{|u| u[0]}.join(',')) | |
end | |
# 将得奖用户 lucky_users 显示在屏幕上 | |
lucky_users.each do |user| | |
puts "姓名:#{user[1]}, 手机尾号:#{user[2]}" | |
sleep 0.5 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment