Velocity Reviews > Ruby > [QUIZ] Unit Conversion (#183)

# [QUIZ] Unit Conversion (#183)

Matthew Moss
Guest
Posts: n/a

 11-14-2008
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

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.

Trans
Guest
Posts: n/a

 11-14-2008

On Nov 14, 10:49=A0am, Matthew Moss <(E-Mail Removed)> 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.

Matthew Moss
Guest
Posts: n/a

 11-14-2008
>>
>> 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.

Sebastian Hungerecker
Guest
Posts: n/a

 11-14-2008
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

Matthew Moss
Guest
Posts: n/a

 11-14-2008

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.

Ryan Davis
Guest
Posts: n/a

 11-15-2008

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

Matthew Moss
Guest
Posts: n/a

 11-15-2008

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.

Todd Benson
Guest
Posts: n/a

 11-15-2008
On Fri, Nov 14, 2008 at 6:12 PM, Ryan Davis <(E-Mail Removed)> 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

Peter Szinek
Guest
Posts: n/a

 11-15-2008

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

>
>
> On Nov 14, 10:49 am, Matthew Moss <(E-Mail Removed)> 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

Sebastian Hungerecker
Guest
Posts: n/a

 11-15-2008
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: (E-Mail Removed)
ICQ: 205544826

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Heidy UK VOIP 1 06-22-2007 09:53 AM VvanN C++ 5 04-28-2006 10:01 AM OCZ Guy DVD Video 6 08-01-2004 05:44 PM OCZ Guy Computer Information 0 07-31-2004 12:29 PM