我有以下方法......
class output
# Converts a hash into a file file.
def to_file(hash, output)
output_file = File.new(output, 'w')
hash.each do |id, seq|
output_file.puts id
output_file.puts sequence
end
output_file.close
end
end
所以当我需要将哈希的内容写入文件时,我会写下以下内容:
to_output = output.new
to_output.to_file(hash_name, output_file_name)
这就是我想要做的......
hash_name.to_file(output_file_name)
这是否可行,这会更好,因为我不需要实例化'output'类......
答案 0 :(得分:2)
定义Hash#to_file
:
class Hash
def to_file(output)
output_file = File.new(output, 'w')
each do |id, seq|
output_file.puts id
output_file.puts seq
end
output_file.close
end
end
的变化:
class output
=> class Hash
hash
已消失。hash.each
成为each
(或self.each
)sequence
=> seq