Facets Units. How do I use it?

Hi all,

I am trying to use the facet units and I am having no luck. If I do

require ‘facets’

and I still can’t use any for the unit examples on the facets site. So
I tried to do

require ‘facet/units’

and I get the following error

ArgumentError: wrong number of arguments (0 for 1)
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:710:in
b' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:710:inclean_eval’
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:710:in
clean_eval' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:753:indecode_conversion’
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:572:in
register_unit' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:720:inregister_prefixed_unit’
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:598:in
register_binary_unit' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:654:inload_yaml’
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:639:in
each' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:639:inload_yaml’
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:646:in
load_yaml' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:639:ineach’
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:639:in
load_yaml' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:521:infrom_yaml’
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:530:in
instance_eval' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:530:ininitialize’
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:520:in
new' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:520:infrom_yaml’
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:812
from
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:811:in
each' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:811 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
from
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in require' from /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facet/units.rb:1 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:32:ingem_original_require’
from
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:32:in `require’

Any ideas on what I am doing wrong?

On 15/06/07, fREW Schmidt [email protected] wrote:

and I get the following error

[snip]

Any ideas on what I am doing wrong?

Thanks for the bug report. This is the easiest way to reproduce your
error:

def b(a)
end
=> nil

require ‘facets/more/units’
ArgumentError: wrong number of arguments (0 for 1)
from
/home/peterv/local/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:710:in
b' from /home/peterv/local/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:710:inclean_eval’
from
/home/peterv/local/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/more/units.rb:710:in
`clean_eval’

Basically, you have a definition of b in Kernel or Object that
interferes with Facets Units. A quick fix would be to undef b:

def b(a)
end
=> nil

undef b
=> nil

require ‘facets/more/units’
=> true

Or you could try to figure out how b got defined, which is probably a
library if you didn’t define it yourself, and just not load that
library if you want to use Units for the moment.

Peter

Ah! Brilliant. I had forgotten about that. In my irbrc I defined a
couple methods to help me with some homework (converting to and from
binary) and one was called b.

If I wanted to load up units sans the web-based conversion rates is
there a way that I could do that without modifying the actual source of
units.rb? I am thinking about using this for an app that I would want
to have really really fast response time but I would want to do some
date math and this looks like it could be helpful for that.

On 16/06/07, Frew S. [email protected] wrote:

Ah! Brilliant. I had forgotten about that. In my irbrc I defined a
couple methods to help me with some homework (converting to and from
binary) and one was called b.

If I wanted to load up units sans the web-based conversion rates is
there a way that I could do that without modifying the actual source of
units.rb? I am thinking about using this for an app that I would want
to have really really fast response time but I would want to do some
date math and this looks like it could be helpful for that.

The exchange rates are not actually the reason for the slow start-up
(the exchanged rates are looked up lazily). Tackling the slow start-up
time is planned for the next version of Units, but at the moment I
still don’t have enough time (I may find some in a month or so).

As for date calculations, currently Units only has a notion of
duration, and it seems only up to days. The current version doesn’t
define interaction with actual dates, like adding a month to a today’s
date and such. You might want to look at Active Support for the
moment.

Peter

The exchange rates are not actually the reason for the slow start-up
(the exchanged rates are looked up lazily). Tackling the slow start-up
time is planned for the next version of Units, but at the moment I
still don’t have enough time (I may find some in a month or so).

As for date calculations, currently Units only has a notion of
duration, and it seems only up to days. The current version doesn’t
define interaction with actual dates, like adding a month to a today’s
date and such. You might want to look at Active Support for the
moment.

Peter

Thanks for the tips! I’ll look into it for sure. I think that the
stuff I am doing is simple enough that I can do it by hand (you know,
minute = 60, hour = 60 * minute, use jurian dates, etc.) I don’t know
if something in my setup is broken or something, but the vast majority
of the examples in the Doc didn’t work at all. Here’s what happened:

1.bit/s + 8.bytes/s
NameError: undefined local variable or method `s’ for main:Object
from (irb):2

(1.bit/s).to(byte/s)
NameError: undefined local variable or method `s’ for main:Object
from (irb):3

1.mile.to(feet)
NameError: undefined local variable or method `feet’ for main:Object
from (irb):4

1.acre.to(yd**2)
NameError: undefined local variable or method `yd’ for main:Object
from (irb):5

1.acre.to(sq_yd)
NameError: undefined local variable or method `sq_yd’ for main:Object
from (irb):6

1.gallon.to(self.L)
NoMethodError: undefined method `L’ for main:Object
from (irb):7

1.lb.to(kg)
NameError: undefined local variable or method `kg’ for main:Object
from (irb):8

1.m.s.to(m.s)
NameError: undefined local variable or method `m’ for main:Object
from (irb):9

1.sq_mi.to(km**2)
NameError: undefined local variable or method `km’ for main:Object
from (irb):10

1.mile.to(km)
NameError: undefined local variable or method `km’ for main:Object
from (irb):11

1.usd.to(twd)
NameError: undefined local variable or method `twd’ for main:Object
from (irb):12

If I did like, 1.bit + 1.byte it would work, but the examples above all
failed apparently.

Hi–

A new version of Units will be out in a week or two as part of Facets
2.0. This is a big release and I have a nunber of things to finish up
first. If you’d like an early-bird release, let me know. I can send
you a copy of the current repository.

The currency exhange rates is still in limbo though. I think we might
move these to a secondary lib (eg. require ‘facets/units/currency’)
and work on them after the next release.

Also, for date/time methods you might want to try

require ‘facets/more/times’

Which is very much like the Active Support lib, both of which are
based on the original work of Rich Kilmer.

T.

Also, for date/time methods you might want to try

require ‘facets/more/times’

Which is very much like the Active Support lib, both of which are
based on the original work of Rich Kilmer.

T.

Thanks much, I will definitely check that out.

I don’t know if this is of use to you, but some people over here were
just doing some cooking stuff and trying to convert tablespoons to
ounces, and cc’s to tablespoons and whatnot. I know that Units is not
supposed to be that kind of deal, but it might be worth it to add in
those things.

Thanks again for all of your help!

On 16/06/07, Frew S. [email protected] wrote:

NameError: undefined local variable or method `s’ for main:Object

1.gallon.to(self.L)
from (irb):10
failed apparently.
You need to include the Units module first (this is to prevent
unwanted name space pollution):

require ‘facets/more/units’
=> true

include Units
=> Object

1.bit/s + 8.bytes/s
=> 65.0 bit/s

Note that the current version of Units contains a few embarrassing
bugs that should be removed in the next release (but when that is, is
not up to me). Also, last time I checked, the web service I used for
the currency conversion has ceased working.

Peter

On Jun 16, 7:11 pm, Frew S. [email protected] wrote:

I don’t know if this is of use to you, but some people over here were
just doing some cooking stuff and trying to convert tablespoons to
ounces, and cc’s to tablespoons and whatnot. I know that Units is not
supposed to be that kind of deal, but it might be worth it to add in
those things.

Good idea. Will put it on the TODOs.

Thanks,
T.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs