详细解读Ruby输出文件信息实现方式
Ruby语言的用途比较广泛,可以轻松灵活的帮助我们实现许多功能需求。编程人员可以很容易熟悉这项语言的应用。在这篇文章中,我们为大家带来了Ruby输出文件信息的一些技巧应用。
数据是随机的,所以每次的记录都会不同。
Ruby输出文件信息代码示例:
- #!/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 =~ /^(.)1111$/
- 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}rn" - 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文件中。
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20