Unit Conversion (#183)


#1

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Q. 2:

  1. Please do not post any solutions or spoiler discussion for this
    quiz until 48 hours have passed from the time on this message.

  2. Support Ruby Q. 2 by submitting ideas as often as you can!
    Visit http://splatbang.com/rubyquiz/.

  3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby T. follow the discussion. Please reply to
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Unit Conversion (#183)

Google added a calculator to its search engine a while back. Enter
“convert 50 miles to kilometers”, or even just “50 mi to km”, and the
first “search” result will tell you that 50 miles is 80.4672
kilometers. This works for units other than length. Try “33 ml to
gal”, “6 hours to minutes”, and"50 stones to lbs", and you’ll see that
Google’s calculator knows a lot of different units and how to convert
between them all.

Your task is to write a units converter script. The input to the
script must be three arguments: the quantity, the source units, and
the destination units. The first example above would be run like this:

 $ ruby convert.rb 50 miles kilometers

Or, using abbreviations:

 $ ruby convert.rb 50 mi km

Support as many units and categories of units (i.e. volume, length,
weight, etc.) as you can, along with appropriate abbreviations for
each unit.


#2

On Nov 14, 10:49 am, Matthew M. removed_email_address@domain.invalid wrote:

  1. Enjoy!
    “convert 50 miles to kilometers”, or even just “50 mi to km”, and the
    $ ruby convert.rb 50 miles kilometers

Or, using abbreviations:

 $ ruby convert.rb 50 mi km

Support as many units and categories of units (i.e. volume, length,
weight, etc.) as you can, along with appropriate abbreviations for
each unit.

This will be interesting. I’m not going to endeavor into myself b/c I
help maintain Stick (http://stick.rubyforge.org) which already does
this (and there’s another lib out there that does it too). But it will
be interesting to see how others approach it.

Thanks,
T.


#3
 $ ruby convert.rb 50 mi km

Support as many units and categories of units (i.e. volume, length,
weight, etc.) as you can, along with appropriate abbreviations for
each unit.

This will be interesting. I’m not going to endeavor into myself b/c I
help maintain Stick (http://stick.rubyforge.org) which already does
this (and there’s another lib out there that does it too). But it will
be interesting to see how others approach it.

Note to everyone else: using another lib, such as stick, is considered
cheating for this quiz. :smiley:


#4

On Nov 14, 2008, at 1:25 PM, Sebastian H. wrote:

Matthew M. wrote:

Note to everyone else: using another lib, such as stick, is
considered
cheating for this quiz.

That’s “another lib which does unit conversion” not “any other lib”,
right?

Yes.
Although, if it’s wholly self-contained, that’s worth some awesome
points.


#5

Matthew M. wrote:

Note to everyone else: using another lib, such as stick, is considered
cheating for this quiz.

That’s “another lib which does unit conversion” not “any other lib”,
right?


#6

On Nov 14, 2008, at 11:20 , Matthew M. wrote:

Note to everyone else: using another lib, such as stick, is
considered cheating for this quiz. :smiley:

so… units isn’t a library… is this cheating?

def units(n, i, o) units "#{n} #{i}" #{o}[/[\d.]+/].to_f; end
=> nil

units 50, :mi, :km
=> 80.4672

:smiley:

I assume it is… but I also assume it isn’t a real contender for the
intent of the quiz in the first place (which is a shame, because this
is much better reuse).


#7

On Nov 14, 2008, at 6:12 PM, Ryan D. wrote:

units 50, :mi, :km
=> 80.4672

:smiley:

I assume it is… but I also assume it isn’t a real contender for
the intent of the quiz in the first place (which is a shame, because
this is much better reuse).

Well, there are a number of past quizzes that would be rather boring
if reuse were always the primary goal. :smiley:

Don’t be boring. :wink:


#8

On Fri, Nov 14, 2008 at 6:12 PM, Ryan D. removed_email_address@domain.invalid
wrote:

units 50, :mi, :km
=> 80.4672

:smiley:

I assume it is… but I also assume it isn’t a real contender for the intent
of the quiz in the first place (which is a shame, because this is much
better reuse).

Don’t forget mass can be converted to energy :slight_smile:

Excellent quiz! Waiting for some good answers!

Todd


#9

Peter S. wrote:

ruby convert.rb x unit1 to unit2

or

ruby convert.rb x long_unit_name even_longer_unit_name

Or
ruby convert.rb x “long unit name” “even longer unit name”
i.e. just assume that ARGV[1] and ARGV[2] are the unit names and let the
user
take care of unit names with spaces in them. Seems the most
uncomplicated
option to me.

HTH,
Sebastian


#10

On 2008.11.14., at 19:55, Trans wrote:

  1. Support Ruby Q. 2 by submitting ideas as often as you can!

Unit Conversion (#183)

Your task is to write a units converter script. The input to the
script must be three arguments: the quantity, the source units, and
the destination units. The first example above would be run like
this:

 $ ruby convert.rb 50 miles kilometers

Or, using abbreviations:

 $ ruby convert.rb 50 mi km

How about

ruby convert.rb 100 mile per minute per second foot per second squared

(read: 100 mile per minute per second to foot per second squared)

Maybe we should allow

ruby convert.rb x unit1 to unit2

or

ruby convert.rb x long_unit_name even_longer_unit_name

to avoid unnecessary complicated (or impossible) parameter parsing?

Cheers,
Peter


http://www.rubyrailways.com


#11

Don’t forget mass can be converted to energy :slight_smile:
And patrol to dollars.
Not to mention distance to time. Well it seems at least, whenever I
ask how far is it away I get answers like 1 hour. :wink:

Excellent quiz! Waiting for some good answers!
1+
Robert


#12

Or
ruby convert.rb x “long unit name” “even longer unit name”
i.e. just assume that ARGV[1] and ARGV[2] are the unit names and let
the user
take care of unit names with spaces in them. Seems the most
uncomplicated
option to me.

D’oh! :slight_smile:

Cheers,
Peter


http://www.rubyrailways.com


#13

On Fri, 14 Nov 2008 14:20:42 -0500, Matthew M. wrote:

Support as many units and categories of units (i.e. volume, length,
weight, etc.) as you can, along with appropriate abbreviations for
each unit.

This will be interesting. I’m not going to endeavor into myself b/c I
help maintain Stick (http://stick.rubyforge.org) which already does
this (and there’s another lib out there that does it too). But it will
be interesting to see how others approach it.

Note to everyone else: using another lib, such as stick, is considered
cheating for this quiz. :smiley:

I disagree, and think that if he actually has a useful lib, he should
show it off. When I wrote Quiz #95 (Code to S-Exp), it was useful to me
to see the rubynode and ParseTree solutions, even if some would have
considered that cheating. I wound up using the rubynode version for the
Ruby 1.8 version of the library I was writing.

(Or maybe if you say it’s cheating, that means he’s allowed to talk
about
it during the spoiler period.)

–Ken


#14

On Nov 15, 2008, at 8:07 PM, Ken B. wrote:

Or, using abbreviations:
this (and there’s another lib out there that does it too). But it
will
be interesting to see how others approach it.

Note to everyone else: using another lib, such as stick, is
considered
cheating for this quiz. :smiley:

I disagree, and think that if he actually has a useful lib, he should
show it off.

It is cheating.

However, I never told him not to do it. :wink:

Seriously, this is Ruby Q… I’m not going to call the cops on you
because you did something other than what I expected.


#15

For what it’s worth, I always invited people to submit whatever they
wanted when I ran the quiz. The more the merrier, I figure.
Beside, I was just going to summarize whatever the heck I wanted to
anyway. :wink:

Agreed. I said it was cheating, but I didn’t say “don’t cheat.” :smiley:


#16

On Nov 15, 2008, at 8:07 PM, Ken B. wrote:

Or, using abbreviations:
this (and there’s another lib out there that does it too). But it
to see the rubynode and ParseTree solutions, even if some would have
considered that cheating. I wound up using the rubynode version for
the
Ruby 1.8 version of the library I was writing.

(Or maybe if you say it’s cheating, that means he’s allowed to talk
about
it during the spoiler period.)

For what it’s worth, I always invited people to submit whatever they
wanted when I ran the quiz. The more the merrier, I figure. Beside,
I was just going to summarize whatever the heck I wanted to anyway. :wink:

James Edward G. II


#17

On Nov 16, 2008, at 11:31 AM, Rubén Medellín wrote:

It would be much more fun to include some non static conversion
variables. Like:
ruby convert.rb 30 euros dollars

Just making things more interesting

Go for it! :smiley:


#18

Agreed. I said it was cheating, but I didn’t say “don’t cheat.” :smiley:

Sorry for nitpicking, but it is not cheating. It might not be what
you want and it is important to know what you consider a valid
submission or not. But cheating induces fraud, deceit or dishonesty. I
guess any solution having a proud
require ‘mylib’
at the top does not have any of these negative properties.

Not that I want to make your job harder than it is already but
solutions that are submitted here might be interesting for the humble
reader even if they do not hit the spirit of the quiz on its
sweet-spot, and you might not review them. Would you tend to agree on
this?

Cheers
Robert

We are all in the gutter, but some of us are looking at the stars.

Oscar Wilde


#19

My converter converts alcoholic beverages by effect *). In addition to
the source and destination beverage it converts the used containers,
too. Google can’t do this yet :wink:

Example:

$ ruby convert.rb 1 bottle of wine into glasses of beer
1 bottle (0.75l) of wine (VOL 12%) effects equal to 6.06 glasses (2.00l)
of beer (VOL 4%)

$ ruby convert.rb 1 bucket of beer into cups of martini
1 bucket (10.00l) of beer (VOL 4%) effects equal to 26.47 cups (2.65l)
of martini (VOL 17%)

Martin

*)

  • Please use results with care!
  • It can’t turn water into wine!

#20

It would be much more fun to include some non static conversion
variables. Like:
ruby convert.rb 30 euros dollars

Just making things more interesting