Main Content
Ruby collections and strings - some new examples Archive - Originally posted on "The Horse's Mouth" - 2010-02-03 07:28:50 - Graham Ellis
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 implemented, you cannot sort those keys - if you try, you get an array or arrays of two elements returned!
[example] - Sorting an array and iterating through it in various ways
[example] - Using an anonymous array to swap over two variables easily
[exampe] - Combining arrays with "and" and "or"
[example] - sorting a hash - (you can't, but this shows how to get the same effective result!)
[example] - multidimensional structures in Ruby - a hash or arrays in this case
Here are some other examples from yesterday too - string handling and exceptions:
[example] - lots of ways of defining and handling strings
[example] - cleaning up strings of text that are user input using string methods and regular expressions
[example] Using a constant to turn debug mode on and off - also a very good example of reading from and processing a file in Ruby
Some other articles
R118 - Ruby Sources and Resources Load path, load and require in Ruby, and a change from 1.8 to 1.9 Loading Ruby classes - where does Ruby look? Sorta sorting a hash, and what if an exception is NOT thrown - Ruby Automed web site testing scripted in Ruby using watir-webdriver Syncronise - software, trains, and buses. Please! This article Cheat Sheet - what do you need for Ruby on Rails? 2006 - Making business a pleasure R111 - Exceptions. Exceptions in Ruby - throwing, catching and using Reading and checking user inputs - first lessons - Ruby Sorta sorting a hash, and what if an exception is NOT thrown - Ruby Exceptions - a fail-safe way of trapping things that may go wrong Ruby - a training example that puts many language elements together to demonstrate the whole Insurance against any errors - Volcanoes and Python Handling unusual and error conditions - exceptions This article Direct access to object variable (attributes) in Ruby String to number conversion with error trapping in Ruby What are exceptions - Python based answer R109 - Strings and Regular Expressions Clarrissa-Marybelle - too long to really fit? Regular Expressions for the petrified - in Ruby Global Regular Expression matching in Ruby (using scan) Ruby - standard operators are overloaded. Perl - they are not Ruby - a teaching example showing many of the language features in short but useful program Matching regular expressions, and substitutions, in Ruby Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP Ruby - examples of regular expressions, inheritance and polymorphism Object Oriented Ruby - new examples This article Neatly formatting results into a table Search and replace in Ruby - Ruby Regular Expressions The dog is not in trouble Regular Expressions in Ruby Ruby to access web services Ruby Programming Course - Saturday and Sunday What are exceptions - Python based answer String interpretation in Ruby Regular expressions made easy - building from components Regular Express Primer Ruby v Perl - interpollating variables puts - opposite of chomp in Ruby String duplication - x in Perl, * in Python and Ruby R107 - Collections (Arrays and Hashes) in Ruby Reading and parsing a JSON object in Ruby Significant work - beyond helloworld in Ruby Shuffling a list - Ruby and Python Ruby - a teaching example showing many of the language features in short but useful program Sorta sorting a hash, and what if an exception is NOT thrown - Ruby All possible combinations from a list (Python) or array (Ruby) Process every member of an array, and sort an array - Ruby Is this number between? Does this list include? - Ruby Creating, extending, traversing and combining Ruby arrays This article What are Ruby Symbols? Sorting arrays and hashes in Ruby Collection objects (array and hash) in Ruby Adding a member to a Hash in Ruby R106 - Input and Output in Ruby Expect with Ruby - a training example to get you started Significant work - beyond helloworld in Ruby Searching through all the files in or below a directory - Ruby, Tcl, Perl Formatting your output - options available in Ruby Exclamation marks and question marks on ruby method names This article Neatly formatting results into a table Opening and reading files - the ruby fundamentals Ruby Programming Course - Saturday and Sunday Some Ruby programming examples from our course