脚本专栏 
首页 > 脚本专栏 > 浏览文章

Ruby进行文件信息输出实例代码

(编辑:jimmy 日期: 2024/11/16 浏览:3 次 )
演示代码:
[code]
#!/usr/bin/env ruby
COUNT = 10
require 'benchmark'
$account_chars = ('A'..'Z').to_a.delete_if {|c| c =~ /[AEIOUY]/ }
#$password_chars = ('0'..'9').to_a
$password_chars = Array.new
$password_chars[0..9] = ('0'..'9').to_a
$password_chars[10..36] = ('A'..'Z').to_a
def account_char
$account_chars[rand($account_chars.length)]
end
def password_char
$password_chars[rand($password_chars.length)]
end
def account_chars
account_chars = []
6.times {|c| account_chars << account_char }
account_chars
end
def password
begin
begin
password = ''
password << password_char
end while password == '0'
4.times { password << password_char }
end while password =~ /^(.)\1\1\1\1$/
password
end
accounts = {}
lines = []
puts Benchmark::CAPTION
puts Benchmark.measure {
COUNT.times do
i = 0
account_chars.inject(accounts) do |a, c|
i += 1
i == 6 ? a[c] = password : a[c] ||= {}
end
end
}
puts Benchmark.measure {
accounts.each do |char, chars|
account = [char]
chars.each do |char, chars|
account << char
chars.each do |char, chars|
account << char
chars.each do |char, chars|
account << char
chars.each do |char, chars|
account << char
chars.each do |char, password|
account << char
lines << "#{account.join}\t#{password}\r\n"
account.pop
end
account.pop
end
account.pop
end
account.pop
end
account.pop
end
end
}
puts Benchmark.measure {
open 'account.txt', 'wb' do |f|
f.print lines.delete_at(rand(lines.length)) until lines.empty?
end
}
运行后输出到: account.txt文件中。
输出信息格式如下:
数据是随机的,所以每次的记录都会不同。
SZQGMC XC6KP
HLVGTZ HCU30
KTSNNF LSNVS
KWMHRQ 3SM07
BTTZCZ DYMUH
DMKPVN GMJHL
KCFKDR X8X8V
VMMJHB X1D65
SRCPGZ T2IP6
WLRMBT 7YA2Y
上一篇:采用UTF-8解决Ruby on Rails程序的中问题
下一篇:ruby 异常处理:ensure
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。