Exercises, examples and other material relating to training module R109. This topic is presented on public courses
Strings and regular expressions are the core of
most Ruby applications and you'll be thrilled at the
broad scope of methods available to you.
| Articles and tips on this subject | updated |
| 3424 | Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP If you divide £10,000 between 17 lucky children, each gets £588.23 ... but if you divide the number 10,000 by the number 17, you get 588.235294117647. How do you format the data to the appropriate number of decimal places?
In Ruby, you can use the % operator on a string - which is an alias ... | 2011-09-08 |
| 2980 | Ruby - examples of regular expressions, inheritance and polymorphism With delegates on public Well House Consultants courses staying in our on-site accommodation, there's time for them to learn a lot more about each other's applications and get a far wider view of the uses of the subject they're with us to learn. There's time for the delegate who wants to extend practicals ... | 2010-10-02 |
| 2623 | Object Oriented Ruby - new examples We had an "object oriented Ruby" morning on today's course. "But everything's an object in Ruby" you'll tell me and you're sort of right (we could argue about what symbols and code blocks are), but there are certain elements of object orientation which require specific coverage.
Here are links to three ... | 2010-02-04 |
| 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 |
| 2614 | Neatly formatting results into a table Would you like to produce a neat table of results, with the columns just wide enough to take the data? That sounds straightforward, but you can't output anything until you've looked through all the lines to see how long each of them will be.
I was talking about this algorithm today, and this evening ... | 2010-02-02 |
| 2608 | Search and replace in Ruby - Ruby Regular Expressions "If you want to replace one part of a string by another in Ruby, you can use the sub method on your string object. The first parameter you give to the method is the string you want to replace, and the second is the string you want to replace it by."
OK - that's the easy bit ... but what if ....
If ... | 2010-02-01 |
| 2295 | The dog is not in trouble "I know I put my papers somewhere" I said to Lisa [wife], and Gypsy [dog] goes off and whimpers in the corner, looking very guilty.
So had she [dog] taken the papers and chewed them? That wasn't the case - she had heard the word "no" (or rather "know") in what I said, and had taken it that she was ... | 2009-07-18 |
| 2293 | Regular Expressions in Ruby Ruby has a wide variety of string handling methods, and a very strong regular expression engine. In one of the example I wrote today ([link] Regular Expression Example in Ruby) I've included a primer on the elements of a Ruby Regular expression as well as sample source code - showing you how you can ... | 2009-07-16 |
| 1887 | Ruby Programming Course - Saturday and Sunday We're so busy during the week at the moment that I'm running an extra Ruby Course this weekend. The main use of Ruby is within "Ruby on Rails", but our course concentrates on the language itself and so it's suitable for people who are using RSpec, cucumber and Watir as well as Rails.
Here are some ... | 2008-11-16 |
| 1891 | Ruby to access web services If you want to use a Ruby program to access an RSS feed (or some other XML or HTML data), you can start with the standard Net::HTTP module ... full (working, tested) example here. That example "just" uses a Get method to get an RSS feed of the latest posts to the First Great Western Coffee Shop Forum ... | 2008-11-16 |
| 1875 | What are exceptions - Python based answer How do you check for run time errors in your program? You'll include tests with if statements in almost every program you write, to ensure that user entered data is reasonable / as you would expect ... but you will also need to do more that that. You'll need to check whether a system function has run ... | 2008-11-08 |
| 1588 | String interpretation in Ruby Ruby's rather clever in terms or how it handles string interpretation. Since variable names only start with special characters sometimes, it can't use Perl's trick of making the $ and @ character special in a string. So instead it uses a #{.....} notation. For example:
puts "Tocker is #{tocker}"
uses ... | 2008-03-22 |
| 1305 | Regular expressions made easy - building from components There seems to be a certain macho desire in many programmer's minds to write a single complicated regular expression to match against an input line, ignorning the structured approach that everyone accepts quite cheerfully in almost every other case. Have a look at this Python line:
wholeline = r"\d\d-...-\d\d\d\d\s+(\d\d):(\d\d):(\d\d.\d\d),\s+(-?\d+\.\d+),\s+(-?\d+\.\d+),(-?\d+\.\d+),\s+(-?\d+\.\d+),(-?\d+\.\d+),\s+(-?\d+\.\d+)"
Impressive, ... | 2007-08-16 |
| 1195 | Regular Express Primer Over the years I've been teaching people about Regular Expressions, I've learnt what does and doesn't work in such tuition. A casual question I saw yesterday got me writing, and I've just posted up a new technical article to the solution centre - see here (it will open in a separate window).
It's ... | 2007-05-21 |
| 987 | Ruby v Perl - interpollating variables When printing in Perl, you can drop a variable into a double quoted string ant it will be interpretter for you:
"--- $number ---"
If you want to perform an operation on the variable, though, it's not as easy - you have to come out of the double quoted string, do the calculation, then start another ... | 2006-12-21 |
| 986 | puts - opposite of chomp in Ruby In Ruby, the chomp method removes the last character of a string if it's a line separator. The puts method adds a new line character on to the output unless there's one already present.
In Perl and other languages, a great deal of time and mental agility is expended in remembering where there are (and ... | 2006-12-15 (short) |
| 970 | String duplication - x in Perl, * in Python and Ruby In Python and Ruby you can duplicate a string by running the multiply operator (*) on a string object, and in perl you can use the x operator. There have been many times when I've looked at this facility and said to myself "very nice, but isn't that a feature looking for a benefit" - in other words, ... | 2006-12-09 (short) |
| bc | Some String Handling |
| d4_1 | Using a string as a collection |
| d4_6 | Formatted printing |
| d4_7 | Running a piped command via backquotes |
| d4_8 | Regular Expression (including documenation) |
| e2 | Embedded variables in strings |
| heredocs.rb | Here Documents in Ruby |
| ii | Formatting example - splitting money between nieces and nephews |
| kiddos | Formatting a table of data |
| kids | Formatting values in Ruby |
| mlre.rb | Commenting a regular expression |
| pareea | Class for email addresses / regex for splitting and checking |
| pc | extracting sections from a match |
| regex_sub.rb | Regular Expression Substitution |
| rex1.rb | Ruby regular expression demo |
| rex2.rb | Splitting at a regular expression |
| shalt | the compact method removes nil elements from an array |
| sop.rb | + and * on strings in Ruby |
| stdo.rb | Using some of ruby's string methods |
| str_types.rb | Ways of writing a string |
| vest | Cleaning up a phone number and checking a postcode |
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.
Anchors, literals, character groups and counts.
Matching in Ruby.
Modifiers i, o, x and m.
Pattern matching variables.
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.