New to programming, my first program!

Hi Ruby world!

Just thought I’d post my first Ruby program and say hi.

I’m a classical pianist/vocalist and have to learn Sol-fa as part of my
aural work at UNI.
This is my first stab at making a program of any use to me, quite
exciting!

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

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 assume I posted this in the right place?)

Il giorno Tue, 10 Jan 2012 21:51:28 +0900
“Luke D.” [email protected] ha scritto:

etc.) it would be good if I knew now before I delved deeper into the
Ruby world!

Just a couple of small improvements:

  • convert the number of notes to sing to a number before storing it in
    the
    how_many variable, rather than doing it every time you use the
    variable:

    how_many = gets.chomp.to_i

    This allows you to remove the .to_i when you use how_many

  • you don’t need to call to_y on the result of rand(7), as it already
    returns
    an integer

Stefano

2012/1/10 Jess Gabriel y Galn [email protected]:

notes[rand(notes.length)]

Or just

notes.sample

:slight_smile:

On Tue, Jan 10, 2012 at 1:51 PM, Luke D. [email protected] wrote:

etc.) it would be good if I knew now before I delved deeper into the
Ruby world!

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 assume I posted this in the right place?)

Attachments:
http://www.ruby-forum.com/attachment/6894/SolFa.rb

Welcome to Ruby !
It’s actually a quite good first program, congratulations.
There are a couple of minor things I would change:

A Hash whose keys are numbers like an index (they don’t have inherent
meaning) resembles too much an Array. In this example I can’t see why
that hash cannot be an Array:

notes = %w{Doh Ray Me Fah Soh Lah Te}

and change the random to

notes[rand(notes.length)]

Also, what the user types is always a number for you, so just
transform it the first time, instead of every time you use it:

how_many = gets.to_i

Now you don’t need the to_i every time you use it.

Other than that, good work !

Jesus.

On Tue, Jan 10, 2012 at 07:51, Luke D. [email protected] wrote:

Hi Ruby world!

Hi Luke!

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.
(Most don’t.)

This is my first stab at making a program of any use to me, quite
exciting!

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

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

Yup!

El 10/01/2012 21:50, “Eric C.” [email protected]
escribi:

2012/1/10 Jess Gabriel y Galn [email protected]:

notes[rand(notes.length)]

Or just

notes.sample

:slight_smile:

Good one, thanks :slight_smile:

Jesus

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs