Main Content

Learning to program in Ruby - examples of the programming basics

Archive - Originally posted on "The Horse's Mouth" - 2009-07-15 07:40:33 - Graham Ellis

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 you name a block of code and re-use it to avoid having to duplicate code and later maintainance work.

On yesterday's Ruby course, I produced a series of example of many of the basics and I have uploaded them on to our web site for you to take a look through if you wish. I have tried to avoid the temptation of "I'll just add something to show xxx" in these examples - the wood should not get lost in the trees!

[link] Prompt, read from user, calculate, output in Ruby

[link] Use of intermediate variables

[link] Use of intermediate variables - alternative

[link] Easier output formatting

[link] Calculations within double quotes

[link] if, elsif and else illustrated

[link] while, until and for loops - comparison

[link] finding extreme values from a collection

[link] Summing a collection

[link] Putting common code in a name block (function)

[link] Define class, create and use instances of it

[link] Use of 'self' in Ruby

[link] Calling program to access class in another file ...

[link] ... and that sample class in its own file

We offer two public Ruby courses - Learning to program in Ruby for newcomers to programming, and Ruby Programming for those with prior programming experience in another language. Public courses run at our Well House Manor training centre in Melksham, Wiltshire, England. These are niche courses - we offer residential facilities to full hotel standard, and can help with delegates travel arrangements too.