Rounding to a Decimal Place with ruby-units

I am working on a project that involves a number of unit conversions and so I started using ruby-units to facilitate.  It is a really flexible library that is really easy to use...I would highly recommend it if you have any need for conversions.  I did run into a small issue with it though.  I needed to take some value, say 160 kg, and convert it to pounds.  That yields the beautiful value of 352.739619 pounds.  I need it to be in a %.1f format, and calling .to_s("%.1f") works great if it is a decimal like that.  I also needed whole numbers, like 160 kg to show up as "160 kg" and not "160.0 kg".  Normally not a big deal.  The good ol' brute force multiply-round-divide trick should work, right?  

Well, not really, unfortunately.  When you divide certain types of Units, it may keep it as a fraction if it doesn't divide evenly.  For example...

("160kg".unit.to('lbs') * 10).round / 10  #=> 3527/10 lbs

That isn't what I want, of course.  After trying to finagle this one way or another, it finally dawned on me that there is a simple, albeit inefficient, solution to this.

"160kg".unit.to('lbs').to_s("%.1f").unit  #=> 352.7 lbs

That's it.  Just to_s and then back to a unit.  It will round off the zero automatically if it was a ".0".

Apache CXF and Variable Paths

In most situations, a REST service would be set up with a concrete path and known number of path elements. On occasion, however, we may need to implement a service with a variable path. Maybe it is for a series of search parameters or, as is my case, an actual file path needs to be provided. There is great support for the known paths. You just type it in the Path annotation and tell it where the variables are. Easy. Well, for unknown paths things get a little hairier, although the solution is still simple and not as ugly as it could be. Let's look at an example for encoding a URL that looks like this:   http://codingfrontier.blogspot.com/subsystem/get/path/to/file/somewhere/in/the/system As always, we start with our Path annotation. We have already set up the path for "subsystem" at the class level, so on the method that we want to receive the multi-element parameter we need to set our Path annotation to accept the rest of the URL, whatever that may be, and do something a little dirty in our receiving method.   Path("get/.*")   public void get(@Context UriInfo uriInfo) {     String pathParam = uriInfo.getPath().replaceAll(".*/get/","");     ...   } Calling uriInfo.getPath() will give us the entire path, so we are using a simple regex to strip the constant portion off the beginning of the string.  This will provide us with the entire rest of the string to do with as we please. Accessing the UriInfo this way is less than desirable, but in this case it gets the job done. If you need a cleaner way of accessing each parameter without having to parse them yourself, you can get to them directly as well.   public void get(@Context UriInfo uriInfo) {     String firstParam = uriInfo.getPathSegments().get(0).getPath();     ...   } It isn't very elegant, but it gives you the flexibility to write some really fancy REST services with dynamic URLs.  I don't normally like to step out of the framework like that, but it is nice to have the flexibility to do so when the need arises.