Ruby進(jìn)行文件信息輸出實(shí)例代碼
[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
}
運(yùn)行后輸出到: account.txt文件中。
輸出信息格式如下:
數(shù)據(jù)是隨機(jī)的,所以每次的記錄都會(huì)不同。
SZQGMC XC6KP
HLVGTZ HCU30
KTSNNF LSNVS
KWMHRQ 3SM07
BTTZCZ DYMUH
DMKPVN GMJHL
KCFKDR X8X8V
VMMJHB X1D65
SRCPGZ T2IP6
WLRMBT 7YA2Y
相關(guān)文章
Ruby升級(jí)后no such file to load -- readline解決辦法
這篇文章主要介紹了Ruby升級(jí)后no such file to load -- readline解決辦法,需要的朋友可以參考下2015-04-04在 Ubuntu 12.04 Server 上安裝部署 Ruby on Rails 應(yīng)用
本教程只適合 Ubuntu Server 用于部署項(xiàng)目到線(xiàn)上,建議使用同樣的 Ubuntu 版本,以免遇到一些版本不同帶來(lái)的問(wèn)題2014-07-07Ruby與Ruby on Rails框架環(huán)境搭建的簡(jiǎn)明教程
這篇文章主要介紹了Ruby與Ruby on Rails框架環(huán)境搭建的簡(jiǎn)明教程,包括RubyGems的升級(jí)與OpenSSL的支持等配置,需要的朋友可以參考下2016-05-05Ruby on Rails中Rack中間件的基礎(chǔ)學(xué)習(xí)教程
Rack是一個(gè)連接Ruby程序與服務(wù)器程序之間的中間件,甚至可以說(shuō)Rails也是在Rack的基礎(chǔ)上建立起來(lái)的,這里我們就來(lái)為大家?guī)?lái)Ruby on Rails中Rack中間件的基礎(chǔ)學(xué)習(xí)教程2016-06-06Ruby簡(jiǎn)明教程之?dāng)?shù)組和Hash介紹
這篇文章主要介紹了Ruby簡(jiǎn)明教程之?dāng)?shù)組和Hash介紹,非常簡(jiǎn)潔的講解,可以作為語(yǔ)法備忘,需要的朋友可以參考下2014-06-06

簡(jiǎn)單介紹Ruby on Rails對(duì)PostgreSQL數(shù)組類(lèi)型的支持

升級(jí)到mac 10.10之后使用pod出現(xiàn)問(wèn)題的解決方法