我正在尝试创建一个方法,将表示字节的整数转换为带有'prettied up'格式的字符串.
这是我的半工作尝试:
class Integer
def to_filesize
{
'B' => 1024,
'KB' => 1024 * 1024,
'MB' => 1024 * 1024 * 1024,
'GB' => 1024 * 1024 * 1024 * 1024,
'TB' => 1024 * 1024 * 1024 * 1024 * 1024
}.each_pair { |e, s| return "#{s / self}#{e}" if self < s }
end
end
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?