spatchcock / quantify Goto Github PK
View Code? Open in Web Editor NEWRuby library for handling physical quantities - that is numbers with units (operations, conversions, comparisons, etc)
License: MIT License
Ruby library for handling physical quantities - that is numbers with units (operations, conversions, comparisons, etc)
License: MIT License
Since units have a fixed definition it might be better to have single, singleton instances of each which all quantities can point to, rather than cloning new instances of each every time. Need to investigate this
I'd love to have i18n support for unit labels. What about switching the label definitions in the config.rb to symbols that are only looked up if one explicitly calls unit.label?
https://github.com/spatchcock/quantify/blob/master/lib/quantify/quantity.rb#L281
Add descriptions to raise statements.
Hi Andrew,
currently I'm in the process of performance tweaking quantify_mongoid. That's when I noticed that Quantity#to_si and Unit#for operations are very slow. Tracking it down further (see https://github.com/cblock/quantify/tree/perf/perf) I found out that Dimensions#to_hash is both expensive in terms of cpu cycles and frequently called when invoking #to_si on a quantity.
Same goes for Module#escape_multi_word_units when invoking Unit#for on complex units.
My feeling is that, e.g. #to_hash could be momoized but when I started refactoring I also noticed that you allow reconfiguration of dimensions and units during runtime. This in turn makes it necessary to invalidate memoized values for #to_hash and other operations.
So my question is: Is it really necessary to support reconfiguration during runtime? I found that you use that in quantify_amee to unregister some units. But wouldn't it be better to load everything once during initialization (e.g. into Singletons) and then not change it until app shuts down?
Also: I'm happy to help out refactoring quantify lib into that direction but I'm not too sure where to start. Any suggestions are greatly appreciated.
Unit inflections should be able to be specified as part of the unit definition.
Commas missing on exception handling in #add_or_subtract and #multiply_or_divide methods. Exceptions not raising correctly.
Empty module; superfluous file
e.g.
distance == length
kilos == kilograms
metre == meter
litre == liter
Empty and superfluous
From cblock
"Dimensions.rb mixes the concept of a single dimension with that of a set of dimensions. Breaking this down into a Dimension.rb and a Dimensions.rb would make both comprehension and testing easier for outsiders like me."
Quantify won't parse '2 tons'. Should it pick a default type of ton to use instead of failing? An option to do this, at least, would be good, to deal with fluffy human input.
This should yield a quantity with units m/s^2, or if you consider N to be "proper" SI, it should do nothing. Instead, it seems to throw Quantify into an infinite loop.
Repro:
(1.N/1.kg).to_si
Cancelling the base units also doesn't seem to be able to "separate" the component units of the Newton:
(1.N/1.kg).cancel_base_units! :kg
=> 1 N/kg
[1] pry(main)> Quantity.parse('1 cubic foot')
=> [1.0 ]
[2] pry(main)> Quantity.parse('1 cubic foot')
NoMethodError: undefined method label' for nil:NilClass from /Users/james/.rvm/gems/ruby-1.8.7-p352@askamee/gems/activesupport-3.1.1/lib/active_support/whiny_nil.rb:48:in
method_missing'
Apparently it's the same for metres.
Pound mole currently pluralizes to "pounds mole" - should be "pound moles"
https://github.com/spatchcock/quantify/blob/master/lib/quantify/unit/unit.rb#L265
There are two clauses, both of which are the same, which is daft. The second should be ":remainder == true"...
I get this error when I push to Heroku:
Gem::Exception: ruby_code case not handled: Syck::PrivateType
An error occurred while installing quantify (3.2.1), and Bundler cannot
continue.
Make sure that `gem install quantify -v '3.2.1'` succeeds before bundling.
Failed to install gems via Bundler.
Heroku push rejected, failed to compile Ruby/rails app
The gem works fine locally and I don't have problems with other gems, any ideas why I get this message using quantify?
Presently, prefixes are constrained to be applied only to 'valid' units (SI -> SI, Non-SI -> Non-SI). SI prefixes cannot be applied to non-SI units.
This is a problem at least in the case of the Litre. The millilitre and centilitre are commonly used units and therefore need to be represented.
The probable solution to this is probably to relax the constraint on applying prefixes. This does, however throw up the problem of unit symbols being conflated with one another, for example kt = kilo-tonne or carat. Perhaps some form of precendence needs to be set (probably in the symbol regex used in Unit.for) so that these conflicts follow some rule of thumb.
Currently:
q = 75.kg #=> "75.0 kg"
q.to 'm' # => nil
Instead of returning nil a unit conversion error should be raised
Currently nil values for quantities are automatically coerced into a 0.0 Float representation. My feeling is that this is not quite right.
Consider a use case where a user enters quantities via html form and chooses not to assign a quantity. After saving the form the quantity would automagically be converted into a 0.0 value (unless some extra logic in controllers / models fixes that).
I'd like to propose Quantity.new(nil,nil).to_s => "nil 1"
i.e. a valid quantity with value nil
and unit unity
.
...and I'm happy to send in an appropriate pull request if you feel that this is a reasonable change to the behavior of the API.
The automatic canceling of units might be an area for improvements.
I consciously did not make all cancelable units cancel automatically as there are cases where we want to preserve the units. For example, and quantity of x kilograms per kilogram of y is technically dimensionless (kg/kg - they cancel) but we might want to retain the context of it being "some mass per mass of something else".
Quantify currently support two systems for handling this:
The second approach is useful, but requires an extra method call which seems a bit much at times. Perhaps a global switch for declaring that all operations should cancel units would provide the right compromise between ease and control?
Any suggestions for how to use this?
While it is appearing to work, I'm getting lots of these:
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:126: warning: instance variable @use_symbol_denominator_syntax not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:126: warning: instance variable @use_symbol_denominator_syntax not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:126: warning: instance variable @use_symbol_denominator_syntax not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.