Exercises, examples and other material relating to training module R103. This topic is presented on public courses Learning to program in Ruby
, Ruby Programming
Ruby hasn't re-invented everything - it's taken
the good basics from variables through "Bodmas" to
statement structure and commenting from best practise
|Articles and tips on this subject||updated|
|3917||BODMAS - the order a computer evaluates arithmetic expressions|
What order does a computer program use to evaluate expressions? If I write
2 + 3 * 4 + 5
does it start off, left to right ...
2 + 3 is 5
5 * 4 is 20
20 + 5 is 25
No! it does not, even though the newcomer might think that was the most natural way for ...
|3758||Ruby - standard operators are overloaded. Perl - they are not|
Ruby has been described to me as "What Perl 5.5 should have been", but that statement is a severe dis-service to Ruby, and to Perl. Ruby is a new language, and if you're moving into Ruby from Perl, you'll do best not to assume even the basics from Perl. A lot has been learend from Perl, for sure, ...
|3430||Sigils - the characters on the start of variable names in Perl, Ruby and Fortran|
A sigil (from Latin sigillum "seal") is a symbol created for a specific magical purpose. A sigil is usually made up of a complex combination of several specific symbols or geometric figures, each with a specific meaning or intent. In computer programming, a sigil is a special symbol attached to a variable ...
|3278||Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java.|
Starting with a clean slate. Are variables initisialised, and if so, how? Even with this fundamental question, languages vary considerably.
C and C++
From my (e)mailbag ...
"""In a piece of code we’ve written we declare an array, but we do not fill the elements with values, we assume (dangerous ...
|2617||Comparing floating point numbers - a word of caution and a solution|
"Think of a number between 5 and 15.
Take away the number you first thought of.
Is the result 7?"
We all played games like that as children, thinking of the number 0 ourselves so that we could do an easy calculation and impress our friends. And the result was 7 ... ...
|2613||Constants in Ruby|
If you want to have a read only variable in Ruby, start its name with a capital letter (and you may like to go with a convention of using capitals right through the variable name).
Note that a Ruby constant - a name starting with a capital - hasa global scope, just like a variable name that starts with ...
|2296||Variable scope - what is it, and how does it Ruby?|
Variables have different "scopes" - in other words, a name that is allocated to a piece of computer memory and subsequently used to refer to that memory may be 'know about" to your program only within a very small area, or much more widely. It's the same IRL ("In Real Life") - consider you, Dad, Graham ...
|2287||Learning to program in Ruby - examples of the programming basics|
We so often overlook the basics of programming, and yet they are so fundamental to good code - understanding things like how widely variables can be seen (also known as variable scope), what happens when you divide two numbers (do you get a decimal result or is the remainder thrown away), and how do ...
|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 ...
Examples from our training material
|bw.rb|| Formatting strings and conversions|
|calc.rb|| Some early gotchas!|
|calcbetter.rb|| A simple calculation in Ruby|
|d2.4|| Easier output formatting in double quoted strings|
|d2.5|| Calculations within double quotes|
|floater|| Floating point rounding errors|
|muck|| Common Ruby Pitfalls|
|print.rb|| Print alternatives|
|rub1|| FAILS to continue a line|
|x01|| Constants in Ruby|
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
Structure of statements and comments.
Variables and constants.
Assignments, calculations, etc.
Integer, float and string formats.
Single and double quotes, here documents, general strings.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
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.