[Ruby] 纯文本查看 复制代码 # frozen_string_literal: true
require 'digest'
class Srr
@@sample_path, @@antivir_name, @@notes = nil, nil, nil
@@deleted, @@fixed, @@missed, @@total = 0, 0, 0, 0
@@SampleHash = Hash.new
def initializer(sample_path, antivir_name, notes)
if sample_path == nil || antivir_name == nil
raise "Error: sample_path or antivirus name is nil"
else
unless Dir.exist?(sample_path)
raise "Error: sample_path is not a directory"
end
@@sample_path = sample_path
@@antivir_name = antivir_name
@@notes = notes
end
def ready()
sample_list = Dir.entries(@@sample_path)
puts sample_list
sample_list.each do |sample|
if sample != "." && sample != ".."
@@total += 1
puts(sample)
data = File.read(@@sample_path + "\\" + sample)
sample_hash = Digest::MD5.hexdigest(data)
@@SampleHash[sample] = sample_hash
end
end
end
def check()
sample_list = Dir.entries(@@sample_path)
sample_list.each do |sample|
if sample != "." && sample != ".."
data = File.read(@@sample_path + "\\" + sample)
sample_hash = Digest::MD5.hexdigest(data)
if @@SampleHash[sample] != sample_hash
@@fixed += 1
else if @@SampleHash[sample] == sample_hash
@@missed += 1
end
end
end
end
@@deleted = @@total - @@fixed - @@missed
return "Deleted: "+ (@@deleted).to_s() +"\nFixed: "+ (@@fixed).to_s() +"\nMissed: "+ (@@missed).to_s() +"\nTotal: "+ (@@total).to_s() +"\nDetection rate: "+ ((@@total - @@missed) / @@total).to_s()
end
end
end
puts("Srr in Ruby v0.1.0 A demo for practising Ruby\n")
puts("Enter the path to the sample: ")
sample_path = gets.chomp
puts("Enter the name of the antivirus: ")
antivir_name = gets.chomp
puts("Enter the notes (Press enter to ignore): ")
notes = gets.chomp
srr = Srr.new
srr.initializer(sample_path, antivir_name, notes)
start_time = Time.now
srr.ready
end_time = Time.now
float = end_time - start_time
print("Finished. Used time: "+ (float).to_s() +" Press enter to check the sample: ")
gets.chomp
start_time2 = Time.now
rrr = srr.check()
end_time2 = Time.now
puts("Finished." + rrr + "Used time: "+ (end_time2 - start_time2).to_s() +"\nPress enter to exit: ")
花15分钟实现了个Ruby的Srr的Demo。但是调Bug调了10分钟。相比之下我竟然不知道写1个小时但是几乎不用改非代码逻辑性Bug的Rust和Ruby哪个是自我折磨。。
一个敢写不敢跑,一个敢跑不敢写
|