# [QUIZ] Unit Conversion (#183)

Discussion in 'Ruby' started by Matthew Moss, Nov 14, 2008.

1. ### Matthew MossGuest

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

The three rules of Ruby Quiz 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 Quiz 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
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.

Matthew Moss, Nov 14, 2008

2. ### TransGuest

Re: Unit Conversion (#183)

On Nov 14, 10:49=A0am, Matthew Moss <> wrote:
> -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=

=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-
>
> The three rules of Ruby Quiz 2:
>
> 1. =A0Please do not post any solutions or spoiler discussion for this
> quiz until 48 hours have passed from the time on this message.
>
> 2. =A0Support Ruby Quiz 2 by submitting ideas as often as you can!
> Visit <http://splatbang.com/rubyquiz/>.
>
> 3. =A0Enjoy!
>
> Suggestion: =A0A [QUIZ] in the subject of emails about the problem
> the original quiz message, if you can.
>
> -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=

=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-
>
> ## Unit Conversion (#183)
>
> Google added a calculator to its search engine a while back. Enter =A0
> "convert 50 miles to kilometers", or even just "50 mi to km", and the =A0
> first "search" result will tell you that 50 miles is 80.4672 =A0
> kilometers. This works for units other than length. Try "33 ml to =A0
> gal", "6 hours to minutes", and"50 stones to lbs", and you'll see that =

=A0
> Google's calculator knows a lot of different units and how to convert =A0
> between them all.
>
> Your task is to write a units converter script. The input to the =A0
> script must be three arguments: the quantity, the source units, and =A0
> the destination units. The first example above would be run like this:
>
> =A0 =A0 =A0\$ ruby convert.rb 50 miles kilometers
>
> Or, using abbreviations:
>
> =A0 =A0 =A0\$ ruby convert.rb 50 mi km
>
> Support as many units and categories of units (i.e. volume, length, =A0
> weight, etc.) as you can, along with appropriate abbreviations for =A0
> 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.

Trans, Nov 14, 2008

3. ### Matthew MossGuest

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

Matthew Moss, Nov 14, 2008
4. ### Sebastian HungereckerGuest

Re: Unit Conversion (#183)

Matthew Moss wrote:
> Note to everyone else: using another lib, such as stick, is considered =A0
> cheating for this quiz.

That's "another lib which does unit conversion" not "any other lib", right?

=2D-=20
Jabber:
ICQ: 205544826

Sebastian Hungerecker, Nov 14, 2008
5. ### Matthew MossGuest

Re: Unit Conversion (#183)

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

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

Matthew Moss, Nov 14, 2008
6. ### Ryan DavisGuest

Re: Unit Conversion (#183)

On Nov 14, 2008, at 11:20 , Matthew Moss wrote:

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

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

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

Ryan Davis, Nov 15, 2008
7. ### Matthew MossGuest

Re: Unit Conversion (#183)

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

>
> On Nov 14, 2008, at 11:20 , Matthew Moss wrote:
>
>>>> Note to everyone else: using another lib, such as stick, is
>>>> considered cheating for this quiz.

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

Don't be boring.

Matthew Moss, Nov 15, 2008
8. ### Todd BensonGuest

Re: Unit Conversion (#183)

On Fri, Nov 14, 2008 at 6:12 PM, Ryan Davis <> wrote:
>
> On Nov 14, 2008, at 11:20 , Matthew Moss wrote:
>
>>>> Note to everyone else: using another lib, such as stick, is considered
>>>> cheating for this quiz.

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

Excellent quiz! Waiting for some good answers!

Todd

Todd Benson, Nov 15, 2008
9. ### Peter SzinekGuest

Re: Unit Conversion (#183)

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

>
>
> On Nov 14, 10:49 am, Matthew Moss <> wrote:
>> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>>
>> The three rules of Ruby Quiz 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 Quiz 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
>> 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

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

Peter Szinek, Nov 15, 2008
10. ### Sebastian HungereckerGuest

Re: Unit Conversion (#183)

Peter Szinek 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
--
Jabber:
ICQ: 205544826

Sebastian Hungerecker, Nov 15, 2008
11. ### Peter SzinekGuest

Re: Unit Conversion (#183)

[Note: parts of this message were removed to make it a legal post.]

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

Cheers,
Peter
___
http://www.rubyrailways.com

Peter Szinek, Nov 15, 2008
12. ### Robert DoberGuest

Re: Unit Conversion (#183)

>
> Don't forget mass can be converted to energy

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.
>
> Excellent quiz! Waiting for some good answers!

1+
Robert

Robert Dober, Nov 15, 2008
13. ### Ken BloomGuest

Re: Unit Conversion (#183)

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

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

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

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

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Ken Bloom, Nov 16, 2008
14. ### Matthew MossGuest

Re: Unit Conversion (#183)

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

> On Fri, 14 Nov 2008 14:20:42 -0500, Matthew Moss wrote:
>
>
>>>> 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.
>>>
>>> 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.

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

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

Matthew Moss, Nov 16, 2008
15. ### James GrayGuest

Re: Unit Conversion (#183)

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

> On Fri, 14 Nov 2008 14:20:42 -0500, Matthew Moss wrote:
>
>
>>>> 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.
>>>
>>> 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.

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

James Edward Gray II

James Gray, Nov 16, 2008
16. ### Matthew MossGuest

Re: Unit Conversion (#183)

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

Agreed. I said it was cheating, but I didn't say "don't cheat."

Matthew Moss, Nov 16, 2008
17. ### Rubén MedellínGuest

Re: Unit Conversion (#183)

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

Rubén Medellín, Nov 16, 2008
18. ### Matthew MossGuest

Re: Unit Conversion (#183)

On Nov 16, 2008, at 11:31 AM, Rub=E9n Medell=EDn 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!

Matthew Moss, Nov 16, 2008
19. ### Robert DoberGuest

Re: Unit Conversion (#183)

> Agreed. I said it was cheating, but I didn't say "don't cheat."
>
>

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

Robert Dober, Nov 17, 2008
20. ### Matthew MossGuest

Re: Unit Conversion (#183)

On Nov 17, 2008, at 4:27 AM, Robert Dober wrote:

>> Agreed. I said it was cheating, but I didn't say "don't cheat."
>>
>>

> Sorry for nitpicking, but it is *not* cheating.

Provide a simple, better word for "going against intended guidelines"
and I'll consider it.

In reality, I think most people understand that it is being used
tongue-in-cheek. And, as I stated, "cheating" is acceptable.

> It might not be what
> you want and it is important to know what you consider a valid
> submission or not.

Basically, anything within reason that solves the quiz problem is
valid, "cheating" or not.

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

How about just writing a solution that solves the quiz -- "cheating"
if you like, not "cheating" if you don't like -- and leave the spirits
to go haunt some Perl coder. I never have time to fully review all the
solutions, "cheating" or not. But I generally give everything a once-
over. Just because you do something that I've called cheating will not
automatically send your submission to the trash bin. On the contrary,
I'll probably point it out as "the right way" to do things.

If your main issue is that you think "cheat" implies dishonesty, then
understand I mean no such thing. gsub(/cheat/, "reuse") and you'll be
happier.

Matthew Moss, Nov 17, 2008