GithubHelp home page GithubHelp logo

Measure: time math about reality HOT 6 OPEN

molybdenum-99 avatar molybdenum-99 commented on May 21, 2024
Measure: time math

from reality.

Comments (6)

ptolemybarnes avatar ptolemybarnes commented on May 21, 2024

Hey @zverok, really cool gem you have here. I'm interested in getting involved. A question on this: why not use an existing gem to handle units rather than roll your own? At a glance, this one might do.

from reality.

zverok avatar zverok commented on May 21, 2024

Hey @ptolemybarnes!
It's REALLY complicated question, faithfully.

For one, current Reality state is compact yet useful prototype. I'd be really happy to use any other library instead of building my owns... But it happens so that existing libraries (of course, I evaluated ruby-units, as well as unitwise, ruby-measurement and several others) are powerful in some areas while helpless in others, that I feel as important.

For example, most of existing libraries are hard at extending their units system, while I (currently) think it is necessary:

require 'reality'
Reality::Entity('Ukraine').population
# => #<Reality::Measure(42,800,501 person)> 

require 'ruby-units'
Unit.new(42_800_501, 'person')
# => ArgumentError: '42800501 person' Unit not recognized

I REALLY want to use something external, and still thinking hard about it. If somebody (wink-wink) will do this carefully, I'd be happy!

My targets for Ideal Eternal Reality::Measure (either constructed independently, or used from external library, or being wrapper on several libraries):

  • no extension of core classes
  • pretty-looking and informative inspect (currently, I think our measure inspect looks more consistent than ruby-unit's one and more concise than unitwise's one, but my mind can be changed with good arguments)
  • math works (1m + 2m, 1km / 1h and so on)
  • conversions works (both implicitly, like 1km + 100m and explicitly, like 1km.to(m))
  • there are helpful synonyms, like 1km vs 1kilometer
  • appropriate fallback to native types:
# ruby-units: nope
(Unit.new(1, 'm') / Unit.new(2, 'm')).class
# => RubyUnits::Unit 

# unitwise: nope
(Unitwise(1, 'm') / Unitwise(2, 'm')).class
# => Unitwise::Measurement

# reality: yep
(Reality::Measure(1, 'm') / Reality::Measure(2, 'm')).class
# => Rational 
  • domain-specific processing; I'm absolutely not sure it should go to "core class", but somehow feel so. Like this tickete, where we are talking: neither ruby-units, nor unitwise can provide extended functionality for "time measurement" units... and I've event not speeking about currencies :) Which are also (from Reality's point of view) somewhere near this topic!

from reality.

zverok avatar zverok commented on May 21, 2024

neither ruby-units, nor unitwise can provide extended functionality for "time measurement" units...

ok, looking closer at it, ruby-units seems to can... though, by extending core classes. which is pretty questionable practice for widely-used gems (unless this gem is rails/active_support, but it's not the case)

from reality.

zverok avatar zverok commented on May 21, 2024
require 'ruby-unit'
Unit.new(1000, 'm') / Unit.new(1, 'm')
# => 1000 --- ok
Unit.new(1, 'km') + Unit.new(1, 'm')
# => 1.001 km --- ok
Unit.new(1, 'km') / Unit.new(1, 'm')
# => 1 km/m --- NOT OK

...and so on, and so on.

from reality.

ptolemybarnes avatar ptolemybarnes commented on May 21, 2024

I see. I can't see how you can have complete extensibility and be able to convert between measurements in like units though. For example, if we want to do:

Measure.new(1, 'km') + Measure.new(1, 'm')

Then the units 'km' and 'm' have to somehow know about each other so that they can know that they're compatible. But the way units are being created at the moment doesn't allow for that. Please correct me on this if I'm wrong; I haven't spent that much time exploring the code yet. :)

from reality.

zverok avatar zverok commented on May 21, 2024

Yes, currently Measure is kind of "early draft" (as almost everything inside). I haven't made up my mind yet of how to provide convertability, there are three major options, as I can see:

  • just have some dictionary (hash) of unit correspondences, and examine it on the fly;
  • make some serious Unit Definition API (like Unitwise and ruby-units do);
  • just use one of them (internally, or instead of Measure, with some custom additions: allow adding units on-the-fly; at least unitwise's author thinks it is principally possible).

Maybe even on-the-fly units is not what we want, but rather predefined list (which should be, though, larger than "just physical units").

So, I'm open to discussions and proposals!

from reality.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.