addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupsimageimagesinstagramlinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1outlookpersonJoin Group on CardStartprice-ribbonImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruseryahoo

N Languages in N Months NYC Message Board › Ruby code practice

Ruby code practice

Richard H.
rharrington
New York, NY
Post #: 1
Hello everyone!

I'm going to start a discussion board about Ruby (and will probably do so with the other languages as well).

Andrew Leung and I have been trading emails back and forth about the random number picking exercise that was in the book, and how to come up with a solution that was most Ruby-like.

We put up some gists on github, which have comments below them describing the code.

Andrew's are here and here and mine is here.

Any comments, or new code, would be welcome!
Julian B.
Julian.
Brooklyn, NY
Post #: 3
Mine as expected is quite similar, and is here: https://github.com/Ju...­
Andrew L.
andrewleung
Group Organizer
New York, NY
Post #: 1
Interesting. I like the way you and Richard did the 'high'/'low' part of the solution.

Just wondering, did you try to use Ruby's 'unless' construct for the while loop? I tried and couldn't get that working properly for some reason.
Richard H.
rharrington
New York, NY
Post #: 2
You have to use 'until', not 'unless', in place of a while loop.

'unless' means 'if not'
'until' means 'while not'

This took some getting used to, for me. I was first introduced to these words with Coffeescript, and found them more confusing than the alternative, but they're growing on me.

Aidan F.
aidanfeldman
Brooklyn, NY
Post #: 14
A bit late to the party, but put mine here: https://github.com/af...­

Richard certainly wins for terseness, but I try to avoid assignment in conditions if I can.
Richard H.
rharrington
New York, NY
Post #: 4
In Aidan's take 3, the use of 1.0/0.0 to create INFINITY is hilarious (and by that I mean interesting).

By the way, Aidan, is checking whether the guess is in the (NUM + 1)..INFINITY range something you'd do in real life, rather than just checking whether NUM is greater than the guess?
Andrew L.
andrewleung
Group Organizer
New York, NY
Post #: 2
Interesting solution Aidan. Just wondering, why do you avoid assignments in conditionals?

Also, I like how your 3rd solution used a case statement with 'INFINITY'. That's a very neat way to do the comparison. I didn't think of doing it like that.
Aidan F.
aidanfeldman
Brooklyn, NY
Post #: 15
No, I definitely wouldn't do that for real- was just experimenting with how to use ranges for the problem.

The assignment in a condition thing isn't Ruby-specific, and it can certainly help cut lines... I dunno, just one of those things that someone told me not to do early on and I took it as gospel :-) I suppose it's easy for it to look like a comparison at a glance, and that it's doing two separate operations in one line. Chaining is one thing since it's building up operations in order, but this would be like adding semicolons and doing everything in one line.
Powered by mvnForum

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy