For 2023 (and 2024 ...) - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
Well House Consultants
You are on the site of
Well House Consultants
who provide
Open Source Training Courses
and business
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
|
Ruby module R107
Collections (Arrays and Hashes) in Ruby
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 | 4502 | Reading and parsing a JSON object in Ruby Here's a series of three new examples, building up from one another, showing the reading of a JSON feed from a remote web site in Ruby, and the analysis and presentation of that data.
The first example - [here] - shows the loading of extra modules to handle reading from the web and the JSON format, ... | 2015-06-02 | 4499 | Significant work - beyond helloworld in Ruby A little program can do a LOT of work! Scenario - I have a web server log file of some 50 Mbytes (the data from one particular day on our server) and the secon field in each line tells me which of our hosted web sites was being visited. The question I was asked - "how many hits on each host?". Solution ... | 2015-05-28 | 4368 | Shuffling a list - Ruby and Python Shuffling a list / array is quite a common requirement. The best way to do it is to take a list of all the items to be shuffled, and to choose one at random as the first member of the output list / array. Then move the last item on the incoming array into the gap left, and choose another from the now ... | 2014-12-30 | 3757 | Ruby - a teaching example showing many of the language features in short but useful program Although the main publicity and driver for the Ruby language has been the Rails web framework (see previous article here), it's an excellent data manipulation language too - with many of the short and efficient coding techniques that you would have available to you in Perl, yet additionally with an object ... | 2012-06-16 (longest) | 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 | randimages | Randomiser of images | 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 | toppers.xx | Hash to count server accesses |
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 [here].
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
Ruby,
Lua,
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.
|
|
|