Where does Ruby load modules from, and how to load from current directoryAll (scripting) languages allow you to load code from other supporting source files, using keywords like use, load, source and include. In Ruby, the most common way to load other code is to require it - and if you use the require method you'll load in code from another file which will be assumed to ...

Separating your code for easier testing, understanding and re-use; example in RubyYou don't want to write the same piece of program twice. Nor do you want to have to call in the technical experts on a particular topic to work on general code. So you should separate your code (whatever language you're using) into named blocks, where the named blocks each represents an element that's ...

Finding sum, minimum, maximum and average in Python (and Ruby)A fresh example (in Python) from today's "learning to program in" course ... finding the number, sum, minimum and maximum of a series of numbers typed in. See [here].
We start off with zero for the count and sum to date, but we do not initialise the minimum or the maximum as we don't know what they ...

Conditionals, loops and methods in Ruby - a primer with simple examplesContinuing with my basic Ruby update - a lot of examples on control structures at [this link].
Conditionals in Ruby include if, unless and case (the equivalent of a switch statement. It's been argued that for a strongly object oriented language a "switch" type structure is not needed, and indeed encourages ...

Learning to program - Loop statements such as whileIf your program always ran each statement just once (indeed skipping over statements which were in blocks in false conditions) it would run very quickly and would have little use. You couldn't (for example) run a program which went through a whole series of results from a database query and displayed ...

Learning to Program - the conditional statement (if)Every language has some sort of conditional statement. That's a way of looking at some sort of setting or status in the program and performing some sort of action based on that setting or status.
such statements take the form ...
if {some sort of condition is true} then {run a group of ...

How a for loop works Java, Perl and other languagesJava, Perl, PHP, Ruby, C, C++, Tcl and many other languages support a "for" loop construct.
When your program enters the top of the loop, it performs the first statement in the brackets ... in this Perl example, that's to initialise the $now variable to 1. It then evaluates the expression given as ...

Finding the total, average, minimum and maximum in a programThere are a number of programming techniques which the experienced coder takes for granted, but which aren't necessarily intuitive for the newcomer. Call them "design techniques" or "design patterns" that need to be learned, if you want some fashionable buzzwords.
• To produce the sum of a stream ...

Muttable v immutable and implications - RubyA muttable object is one which can be changed in situ, and an immutable object is one that can't be changed in situ - if you want a modified version, you have to build it afresh, using whichever components you require from the orginal. And if the original's not required, you can then scrap it.
There's ...

Does a for loop evaluate its end condition once, or on every iteration?All the languages that we teach have a for loop or the equivalent, which is a clean way of repeating a block of code with a rising or falling index number. It's used in many circumstances - for example in iterating through the months of the year (for m goes from 1 to 12) of in stepping through all ...

Ruby v Perl - a comparison exampleOne of the popular exercises I set on our Perl courses goes something like ...
Write a program to ask the user to enter four numbers each between the value of 1 and 6. If the user enters a number below 1 or over 6, ask him to enter that number again. If the user enters the word END, stop reading numbers.
Print ...

Assigning values to variables within other statements - RubyIn Ruby, and in Perl too, you can use assignments within other statements and in so doing perform two distinct actions in one line of code. Here's what I mean (using irb - interactive Ruby):
>> j = 16
=> 16
>> print "The value is #{k=j} at the moment"
The ...

Multiple inputs, multiple out, ruby functionsHas it ever struck you as curious that in many languages, you can pass as many parameters into a function as you like, but you can only return one? Well - that's many language not all - in Ruby you can return a whole series of values in a comma separated list, and place each into its own variable.
You ...

Is this number between? Does this list include? - RubyThere'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 ...

Splitting data reading code from data processing code - RubyAn iterator (a.k.a. generator in Python) is a function which returns its results as it calculates them, rather than building them up into a larger structure to return all at once when the function is completed. So where you have a big flow of incoming data, you can handle it as it arrives rather than ...

Ruby training - some fresh examples for string handling applicationsRuby's a great language. No - let me rephrase that "Ruby's a fantastic language" ... for many tasks such as "data munging" - handling / manipulating large flows of data, in all sorts of ways. But in my training role, I come across far more people using Ruby on Rails, Selenium and Watir than Ruby for ...

Returning multiple values from a function call in various languages - a comparisonI've always thought it a bit odd that you call a function with any number of parameters, and yet it returns a single value; in Java, C or C++ you declare a return type (void if there is not to be anything returned) and you are then constrained by that specification. There are, of course, other ways ...

Breaking a loop - Ruby and other languagesWhen you're in a loop there are occasions you want to say get me our of this loop NOW, or "I'm done with the current iteration. And those are the 'classic' break and continue statements from C, C++ and Java. Languages like Perl changed break to last and continue to next ... and added a redo that asks ...

Why do I need brackets in Ruby ... or Perl, Python, C or JavaIn many languages, you're required to put brackets around the boolean condition in your if and while statements, and around parameters you're passing to functions. And it becomes the natural way of coding for programmers in languages like Java, C and C++. But when you think about it, those brackets ...

Alternative loops and conditionals in Ruby and PerlRuby gets a lot from Perl - including its eclecticness in having a wide variety of alternative ways of doing similar things - and no more so than in loops and conditionals. Not only do you have an if but also an unless. Not only do you have a while but also an until. And not only can you write these ...

For loop - checked once, or evety time? Ruby v Perl comparison and contrastAlthough may aspects of Ruby are inherited (in a non-OO way!) from Perl, there are some distinct differences too; a classic for loop in Perl has its end condition checked every time around the loop, but a Ruby for loop sets up an iterator at the start, so that if something changes within the loop evaluation ...

Passing code to procedures and yield in RubyIf you want to pass a block of code into a Ruby procedure, you can do so by passing in the code to a variable who's name you start with an & in the procedure definition; you can then run that code using the .call method on the received object. In some ways this is an indirect reference - in Ruby ...

A short form of if ... then ... elseThere are so many times you want to say "if ... then ... else" to do no more than choose between the word "is" and "are", to say "child" or "children", or to say "may" or "may not" in your output. Using an if statement for that's a lot of code for a little job.
In most of the languages we teach (Perl, ...

Learning to program in Ruby - examples of the programming basicsWe 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 ...

Ruby, Perl, Linux, MySQL - some training notesWe have just come to the end of a solid 12 days of training ... and we are just getting ready for the next week, with delegates arriving this evening. There's a lot going on behind the scenes, even over this weekend, with the hotel to be prepared as well as the training course. (Picture - our clean ...

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 ...

Ruby to access web servicesIf 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 ...

What to do with a huge crop of applesLast year, you had a good crop of apples on your tree .... what did you do with them? Make apple pies for all the neighbours! How?
• You collect all the apples and bring them into the kitchen.
• You prepare the apples ready for the pies.
• You make the pastry and apple pies.
• You ...

Clean code, jump free (Example in Lua)The "goto" statement - in languages that still support it - is regarded with disdain by Computer Scientists as it makes for spaghetti code - hard to follow and going all over the place!
In some ways, loop controls "break" and "continue" ("last", "next" and "redo" in Perl; "next", "redo", "retry" and ...

Saying NOT in Perl, PHP, Python, Lua ..."Isn't there one standard way to say NOT?" asked one of my delegates on today's course - and it's an excellent question. But the answer to a question about a negative subject is itself in the negative - no, there isn't just a single way!
In fact .. I can think of no fewer that 12 ways!
• 1. ! ...

Some Ruby programming examples from our courseI was giving a Public Ruby Course to a small group at the end of last week ... and having a small group gave me the opportunity to write some demonstrations in front of them. I have now tidied these up and have pleasure in presenting to more Ruby demonstrations:
Ruby's BEGIN block
The compact method ...

Ruby, C, Java and more - getting out of loopsbreak and continue statements have been available for loop control for many years, and others functionallity has been added such as Perl's redo. From today's Ruby Course, here's a table that compares these loop controls in Ruby to similar commands in the other languages that I have been discussing ...

for loop - how it works (Perl, PHP, Java, C, etc)When writing a program, you'll often want to repeat a block of code, counting up through a table or performing a block of code with an input value (loop counter) that goes up 1, 2, 3, 4 etc.
You COULD do this using a while loop, but this means you have to specify each of
• how to start (initialise) ...

A better alternative to cutting and pasting codeIf you're new to coding, you'll be so concerned to be writing code that works that you may not take a look at coding technique. Your nose will be so close to the grindstone as you work that you won't take the time to look and ask "Do I need to keep grinding anyway?"
If you find yourself writing a piece ...

Ruby's case - no breakIt has always struck me that switch statements in languages like C, PHP, Tcl and Java are very longwinded and clumsy, with a need for blocks and blocks, and every case having to end with a break (or equivalent) to stop multiple cases being run via a drop-through. I can understand why they were written ...

Equality in Ruby - == eql? and equal?The == comparison checks whether two values are equal
eql? checks if two values are equal and of the same type
equal? checks if two things are one and the same object.
How do I remember which is which ... The longer the operator, the more restrictive the test it performs
Example:
irb(main):013:0> ...

1st, 2nd, 3rd revisited in RubyI've been updating some of my Ruby examples today (as one does on a Saturday!) and I thought back to the post I made yesteday concerning 1st, 2nd, 3rd, etc in Python.
Ruby has a case statement, even though Ruby is a bit of a cross between Python and Perl, neither of which supports the structure. Historically, ...