# Facets Units. How do I use it?

**URL:** <https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971>\
**Category:** Ruby\
**Created:** [June 15, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971 "2007-06-15T22:41:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fREW\_Schmidt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fREW\_Schmidt](https://www.ruby-forum.com/u/fREW_Schmidt)\
**Post date:** [June 15, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971/1 "2007-06-15T22:41:50Z")

</div>

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:in`clean\_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:in`decode\_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:in`register\_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:in`load\_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:in`load\_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:in`each’  
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:in`from\_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:in`initialize’  
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:in`from\_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:in`gem\_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:in`gem\_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?

---

<div class="post-metadata">

**Author:** ![fREW\_Schmidt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fREW\_Schmidt](https://www.ruby-forum.com/u/fREW_Schmidt)\
**Post date:** [June 15, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971/2 "2007-06-15T23:48:16Z")

</div>

On 15/06/07, fREW Schmidt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:in `clean\_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

---

<div class="post-metadata">

**Author:** ![fREW\_Schmidt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fREW\_Schmidt](https://www.ruby-forum.com/u/fREW_Schmidt)\
**Post date:** [June 16, 2007, 12:34am UTC](https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971/3 "2007-06-16T00:34:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fREW\_Schmidt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fREW\_Schmidt](https://www.ruby-forum.com/u/fREW_Schmidt)\
**Post date:** [June 16, 2007, 1:37am UTC](https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971/4 "2007-06-16T01:37:30Z")

</div>

On 16/06/07, Frew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![fREW\_Schmidt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fREW\_Schmidt](https://www.ruby-forum.com/u/fREW_Schmidt)\
**Post date:** [June 16, 2007, 3:32am UTC](https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971/5 "2007-06-16T03:32:00Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![fREW\_Schmidt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fREW\_Schmidt](https://www.ruby-forum.com/u/fREW_Schmidt)\
**Post date:** [June 16, 2007, 7:04am UTC](https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971/6 "2007-06-16T07:04:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fREW\_Schmidt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fREW\_Schmidt](https://www.ruby-forum.com/u/fREW_Schmidt)\
**Post date:** [June 17, 2007, 1:11am UTC](https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971/7 "2007-06-17T01:11:23Z")

</div>

> 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!

---

<div class="post-metadata">

**Author:** ![fREW\_Schmidt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fREW\_Schmidt](https://www.ruby-forum.com/u/fREW_Schmidt)\
**Post date:** [June 16, 2007, 4:05am UTC](https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971/8 "2007-06-16T04:05:41Z")

</div>

On 16/06/07, Frew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![fREW\_Schmidt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f05b48/32.png) [@fREW\_Schmidt](https://www.ruby-forum.com/u/fREW_Schmidt)\
**Post date:** [June 17, 2007, 6:18am UTC](https://www.ruby-forum.com/t/facets-units-how-do-i-use-it/106971/9 "2007-06-17T06:18:28Z")

</div>

On Jun 16, 7:11 pm, Frew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
