dominikh / filesize

filesize is a small ruby class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.
MIT License
81 stars 16 forks source link

Added bit type #3

Closed imerr closed 9 years ago

imerr commented 10 years ago

Commonly used for network bandwidths (that's what I need it for anyways)

Hope the code isn't too ugly, feel free to improve it/suggest improvements - I'm a ruby newbie

quick example

irb(main):003:0* Filesize.from('100mbit').to('MB')
=> 12.5
irb(main):004:0> Filesize.from('12.5MB').to('MBit')
=> 100.0
dominikh commented 10 years ago

Will decide on this change later, but it definitely makes me regret that the existing regexps are case insensitive, because MB is a megabyte, whereas Mb would be a megabit. Mbit reads a bit funny to me.

imerr commented 10 years ago

I think it being very explicit is actually a good thing if you use it to parse user-input. I'd imagine lots of people just putting it in all lower case, this prevents accidental misunderstandings - not to mention bit isnt exactly a unit people use a lot.

dominikh commented 10 years ago

not to mention bit isnt exactly a unit people use a lot

It's also technically not a unit used for file sizes, but of course it's used in combination with file sizes (as in your use case). I'm neither happy about case insensitive units, nor about supporting "Mbit" but not "Mb" to mean Megabit, but we cannot change it to being case sensitive now, as that'd probably break a lot of code, and users do indeed often not use appropriate casing.

dominikh commented 9 years ago

Sorry, but I don't think I can merge this as long as filesize ignores casing. If we supported Mbit, people might use Mb expecting it to mean Mbit. But instead, their values would be treated as MB, that can be a rather problematic bug.

If we ever release another, backwards incompatible, version of filesize that respects casing, we'll include bit-sizes.