halogenandtoast / alchemist

A scientific conversion library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gigahz (or *hz) fails

toobulkeh opened this issue · comments

Seems that hz.to returns FLOAT Object only. Not sure why (I'm new to ruby) or else I'd help with my own code.

Thanks for the cool library!

Could you provide an example?

irb(main):002:0> 1.hz.to
=> 1.0
irb(main):003:0> 1.hz.to.class
=> Alchemist::NumericConversion

You don't need to call to unless converting to something though

irb(main):001:0> 1.hz.to.rpm
=> 60.0

to always expects the next method to be called to be the unit to convert to.

Sorry I never responded... 2 months is a long time to pay attention to a bug.

I'm returning to this problem and just am trying to use this gem to print some friendly versions of numbers... back to doing things manually though because it simply doesn't seem to work. Here's some examples of converting scale:

irb(main):014:0> 1.bytes.to.mb
NoMethodError: undefined method `to' for 1:Fixnum
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/alchemist-0.1.5/lib/alchemist/numeric_ext.rb:5:in `method_missing'
    from (irb):14
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

irb(main):016:0> 1.0.bytes.to.megabytes
NoMethodError: undefined method `to' for 1.0:Float
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/alchemist-0.1.5/lib/alchemist/numeric_ext.rb:5:in `method_missing'
    from (irb):16
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):017:0> 100.hz.to.gigahz
NoMethodError: undefined method `gigahz' for 100.0:Float
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/alchemist-0.1.5/lib/alchemist/numeric_ext.rb:5:in `method_missing'
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/alchemist-0.1.5/lib/alchemist/numeric_conversion.rb:141:in `perform_conversion_method'
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/alchemist-0.1.5/lib/alchemist/numeric_conversion.rb:122:in `method_missing'
    from (irb):17
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

@toobulkeh Not sure what's happening for you here. There are a couple of things going on in your code though. First in example 1, mb is not a valid short name, it should be:

irb(main):014:0> 1.bytes.to.Mb
=> #<Alchemist::NumericConversion:0x007fbfe1844858 @value=7.62939453125e-06, @unit_name=:b, @exponent=1.0>

Example 2 should work just fine:

› irb
irb(main):001:0> require 'alchemist'
=> true
irb(main):002:0> 1.0.bytes.to.megabytes
=> #<Alchemist::NumericConversion:0x007fe9ca3bdfd8 @value=9.5367431640625e-07, @unit_name=:bytes, @exponent=1.0>

Do you have some other gem or functionality interacting?

Example 3 is something to be fixed on my end, but currently frequency doesn't support the binary prefixes like Giga.

def self.use_binary_prefix? unit
  !use_si && measurement_for(unit).include?(:information_storage)
end

I need to extend this list with frequency and other measurements, but the prefix has a different meaning when not talking about information storage i.e. 10^9 vs 2^30.

By IRB that's Rails console. Works in normal IRB but require 'alchemist' in rails console returns false. Any ideas?

false means it's already loaded, apparently.

still doesn't seem to work though:

vagrant@rdbox:/vagrant/capacity$ irb
irb(main):001:0> require 'alchemist'
=> true
irb(main):002:0> 10000.bytes.to.Mb
=> 0.0762939453125
irb(main):003:0> exit
vagrant@rdbox:/vagrant/capacity$ rails console
Loading development environment (Rails 3.2.13)
irb(main):001:0> 10000.bytes.to.Mb
NoMethodError: undefined method `to' for 10000:Fixnum
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/alchemist-0.1.5/lib/alchemist/numeric_ext.rb:5:in `method_missing'
    from (irb):1
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

I'll look into the interactions with rails.

This is because rails defines a bytes method:

http://api.rubyonrails.org/classes/Numeric.html#method-i-bytes

The way to avoid running into this issue is to use Alchemist.measurement

Alchemist.measurement(1, :byte).to.megabytes