服务器之家

服务器之家 > 正文

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

时间:2020-04-10 15:01     来源/作者:ruby教程网

演示代码:

[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

标签:

相关文章

热门资讯

沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部