-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert a Bytes-based object to a human readable string #4
base: master
Are you sure you want to change the base?
Conversation
Interesting approach. I would like to merge what you have here but we need to take care of some points before. bytes = Humanize::Byte.new(1024)
bytes.to_p.to_s This converts the original object to a With your approach: bytes = Humanize::Byte.new(1024)
bytes.to_human We need to pass a value that is big enough to convert to a Also, if we follow this approach i would like to change it a bit. I would not expect a method called So what about we get rid of Also the constants solution is really nice. Maybe we can do that for all objects?
And please don't bump the library version in the same PR as a feature request. Leave it to be done by the maintainer that is going to generate a new release. Thanks for your interest in making this library better. I hope we can work together to figure the best way to move forward with this! |
Thanks for the feedback! Sounds like there are really 2 potential use cases to solve for
bytes = Humanize::Byte.new(2000)
bytes.to_m.to_s
=> "0.0019073486328125"
gb = Humanize::Giga.new(3.14)
gb.to_m.to_s
=> "3215.36"
bytes = Humanize::Byte.new(2000)
bytes.to_human_s
=> "1.95 KB"
bytes = Humanize::Byte.new(123456789)
bytes.to_human_s
=> "117.74 MB"
# This actually doesn't work in the PR
mb = Humanize::Mega.new(3215.36)
mb.to_human_s
=> "3.14 GB" Since a user creates an object of a specific measure, using bytes = Humanize::Byte.new(2000)
bytes.to_m.to_s
=> "0.0019073486328125"
bytes.humanize
=> "1.95 KB"
mb = Humanize::Mega.new(3215.36)
mb.to_s
=> "3215.36"
mb.humanize
=> "3.14 GB" I will go back to the drawing board on this if you approve. I will submit another PR based on master that constant-izes all of the files KILO = 1024**1
MEGA = 1024**2
GIGA = 1024**3
TERA = 1024**4
PETA = 1024**5
EXA = 1024**6
ZETTA = 1024**7
YOTTA = 1024**8 |
I agree with you here. Those are 2 different approaches and rather then changing the current behaviour adding it as a new feature to the library would be awesome. bytes = Humanize::Byte.new(2000)
bytes.to_m.to_s
=> "0.0019073486328125"
bytes.human.to_s
=> "1.95 KB" With this approach we will improve the original WDYT? |
I like it! Should we close the PR then? |
Hmmm. I think you can just push your new changes to The same branch and it Em qua, 5 de out de 2016 12:39, marshallmick007 [email protected]
|
Ah, but I was going to go back to your master and ditch my changes and start from scratch so things would be cleaner from your end. Up to you if you want to close this and have me reopen a new PR. |
Ah, just get a new branch from mine and do a push force then. =) Em qua, 5 de out de 2016 12:52, marshallmick007 [email protected]
|
Would you expect that the Humanize::Mega.new(10098).human.to_s
=> "9.861328125 GB"
Humanize::Mega.new(10098).to_k.to_s
=> "10340352 KB"
Humanize::Mega.new(10098).to_b.to_s
=> "10588520448 Bytes"
Humanize::Mega.new(10098).to_t.to_s
=> "0.009630203247070312 TB" One of the reasons I didn't originally re-use the The other approach would be to have the |
You are right. This would break the current behaviour. What we can do is accept another parameter on |
Or Maybe we can create a configuration where they can define if they want it or not. |
The more I am thinking about this, the more I like the idea of creating a new object whose only responsibility is to find the best object to convert to. This leaves the existing API intact, and adds the new (optional) functionality without polluting the code or base class with unnecessary logic. class HumanizedMeasurement
def initialize(measure)
@measure = humanize(measure)
end
def to_s
@measure.to_s + " " + @measure.label
end
private
def humanize(measure)
# convert measure to the most appropriate object
end
end Then in Bytes, def human
HumanizedMeasurement.new(self)
end |
Dunno. My concern here is that we might be creating something new just for the sake of keeping the current api intact. |
Here's an example
You can also just use the Bytes class to get a humanized string: