Learn Ruby the Hard Way – Exercise 34 Accessing Elements of Arrays

Here is my work for Exercise 34.

I first answered the questions in strings and use puts to print them to the screen. I then wrote a function check_place(ordinal) that returns a string with the ordinal and cardinal numbers of the specified animal. The function takes an ordinal number as an argument.

Exercise 34 code
Exercise 34 code
Exercise 34 output
Exercise 34 output

Study Drills
1. With what you know of the difference between these types of numbers, can you explain why the year 2010 in “January 1, 2010,” really is 2010 and not 2009? (Hint: you can’t pick years at random.)

Is it because the Gregorian calendar doesn’t start at year zero? Not too sure.
2. Write some more arrays and work out similar indexes until you can translate them.
I feel pretty confident with arrays and indexes, but here’s one more.

Fairy tales, ordinal number and cardinal number.
Fairy tales, ordinal number and cardinal number.

3. Use Ruby to check your answers.

Fairy tales output!
Fairy tales output!

Done!

Summary
Although I am familiar with the concept of arrays and indexes, I had actually not heard of the terms ‘cardinal’ and ‘ordinal’. Zed Shaw makes the terms easy to remember, cardinal for ‘card at random’ (this is the 0 index) and ordinal for ‘being in order’.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s