Exercises, examples and other material relating to training module R107. This topic is presented on public courses
Learning to program in Ruby,
Ruby Programming
Background
If you want to hold many values in a Ruby
object, you'll use and array or a hash - two
powerful structures each of which has a mass
of methods to make them flexible, and a great
deal of syntactic sugar to make them easy to
use.
| Articles and tips on this subject | updated |
| 3435 | Sorta sorting a hash, and what if an exception is NOT thrown - Ruby Some gems from last week ...
1. Ruby's begin and rescue clauses allow you to catch anything exceptional that could derail your program. But did you know that you can also add an else clause to be run only in the event of there being no exception? There's an example from last week's Ruby course which ... | 2011-09-12 |
| 3257 | All possible combinations from a list (Python) or array (Ruby) If there are eight of us at a meeting, how many possibilities are there for a pair of people to stongly disagree? The answer turns out to be 28. And if you look at subgoups of 3, looking for everyone to have the same opinion within the subgroup, the answer is many more.
If there are eight stations ... | 2011-04-23 |
| 3255 | Process every member of an array, and sort an array - Ruby If you're wanting to process every member of an array, you've a choice ... you can write a loop to step through the key of each member, you can write a loop to step through each member itself, or you can call a method which operates on the array as a whole (i.e. the loop is hidden within a method. ... | 2011-04-21 |
| 3253 | Is this number between? Does this list include? - Ruby There's often a requirement in a program to see if one value is between two others, and in most languages you'll write that as as double condition:
if (n >= 5 && n <= 8) printf("Yesssss!\n");
in C or C++, for example.
Some languages give you further options / methods you ... | 2011-04-19 |
| 2976 | Creating, extending, traversing and combining Ruby arrays Ruby shares many of its eclecticism's with Perl - lots of ways of doing the same thing, though it doesn't go quite so far (some would say so far beyond the reasonable) as Perl does. So when you come to create or manipulate an array (really an ordered list) there's a big choice.
indian = ... | 2010-10-01 |
| 2621 | Ruby collections and strings - some new examples From yesterday's Ruby course - a whole set of new examples on Ruby collections - arrays (which are ordered lists) and hashes (which are unordered). In other words, you look somethng up in an array by its position number, whereas you look something up in a hash based on its unique key, and the way it's ... | 2010-02-03 |
| 2618 | What are Ruby Symbols? Ruby Symbols are names that follow a : character. They're a quick way of getting at data, where (for example) you have a fixed key or name in a hash, and you'll find them commonly used in many definitions such as in Ruby on Rails. I've put a short example [here] ... | 2010-02-02 (short) |
| 2606 | Sorting arrays and hashes in Ruby In Ruby, you can use the sort method on an array to sort that array - but you cannot sort a hash. That's because of the techniques used within a hash to make it very fast to look up individual elements.
So what do you do if you want to sort something like this: ?
stuff = {"Steve" => "Kent", "Graham" ... | 2010-01-30 |
| 2291 | Collection objects (array and hash) in Ruby Ruby has two collection objects - Arrays (where you access elements based on their numeric position) and hashes (where you can access elements based on a key, rarely numeric). That's similar in all but name to many other languages, although the names may be different (arrays or lists; hashes or dictionaries ... | 2009-07-16 |
| 991 | Adding a member to a Hash in Ruby In Ruby, you must initialise your variables - in other words, you cannot use the content of a variable that doesn't exist and have the language assume it will be 0, as happened with Perl.
So if - for example - you're using a Hash to keep tabs of a number of counters, you can't just +=1 members and have ... | 2006-12-21 |
Examples from our training material
| a1.rb | First Array |
| a2.rb | Basic Array manipulation |
| a3.rb | methods on arrays |
| amaze4 | Array of counters - log file statistics |
| arar.rb | Arrays of arrays |
| bone | Combining arrays |
| d3_4 | Arrays, and iterating through them |
| d3_5 | Array methods such as grep |
| d3_6 | Array of arrays |
| d3_7 | Setting up and using a hash |
| d3_8 | Simple but practical hash example |
| d3_9 | Using a hash to count web server accesses |
| ddd | Symbols v strings |
| favr | Defining, adding to, iterating through an array |
| h1.rb | First hash in Ruby |
| h2.rb | First hash in Ruby |
| kvp | Sorting in Ruby |
| logan | Read an Apache httpd access log file and count statuses |
| lunch | array sorting and iterating |
| mar | Combining arrays - element by element "and" and "or" |
| others | Symbols |
| pophand.rb | Read, interpret a data file |
| pw.rb | %w to set up an array, and compact |
| pwc.rb | Copying an object in Ruby |
| rrs | Sorting the keys of a hash |
| swapper | Swapping two variables |
| tessa | all possible combinations from an array |
| tim | A Hash of arrays in ruby |
Background information
Some modules are
available for download as a sample of our material or under an
Open Training Notes License for free download from
http://www.training-notes.co.uk.
Topics covered in this module
What is a collection?
Arrays and hashes.
Constructing an array. The %w shortcut.
Nesting arrays.
Hash keys, iterators, etc.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.