On Tue, Jan 10, 2012 at 07:51, Luke D. [email protected] wrote:
Hi Ruby world!
Just thought I’d post my first Ruby program and say hi.
Well done! I like your use of meaningful variable names (something
beginners usually haven’t learned yet), and you’ve picked up nicely
some of the Ruby idioms (like .times) that some people have trouble
with, when they come from languages that don’t have such things.
This is my first stab at making a program of any use to me, quite
Yup, it’s amazing how useful even a short program can be. That’s one
of the reasons for the success of “scripting” languages like Ruby.
You can do awesome things in even just one line, that can save a
system administrator hours of typing to do something manually.
If anyone has some suggestions (features, simplify code, bad commenting
etc.) it would be good if I knew now before I delved deeper into the
The only thing I’d mention that someone else hasn’t, is the indentation.
First, the Ruby community seems to prefer less indentation. You’ve
used tabs, which in most contexts will expand to 4, 5, or 8 spaces.
We usually use 2. But that’s just a matter of taste and convention,
not anything you’ve done wrong! I used to be in the habit of using
tabs myself, both so it was clearer and to discourage too-deep
nesting. (It also took up less disk space and transmission time, back
when those were expensive.)
Second, I see you’ve indented the entire “if” block an extra level.
You may find that keeping the indentation consistent helps you figure
out how many levels deep you are in the control flow (i.e., within
things like times, if, while, and so on). Luckily, the Ruby community
also seems to prefer shorter methods, so such confusion will be rare,
but still, consistency will help anyone trying to understand your
code. (Having a care for the next poor sod to have to look at your
code has been one of my major points over the past couple decades, and
I was very glad to see that the Ruby community places such high
emphasis on clean and readable code.)
The program asks the user how many notes he wants to sing, then randomly
chooses those notes from a hash. The idea is that the user then sings
the Sol-fa presented to them on the screen.
I was wondering: why does it print “doh” before the random notes? Is
that as sort of a “baseline” so that someone listening, who might not
have perfect pitch, can still at least judge the interval?
(I assume I posted this in the right place?)