What is Ruby?
Ruby is an open source programming language. Like many programming languages, with Ruby you can carry out a number of tasks across different software elements and within various applications. You can also create standalone applications using Ruby.
Why Ruby with ICM?
Why Ruby and not another programming language is a question for ICM, but as Ruby is an open source language it’s perhaps a good choice, and easy for developers / modellers to get practice with. Aside from ICM, if you wanted to run Ruby on your machine, you would need to download Ruby and install it onto your machine. With ICM that’s not necessary, and you can run Ruby directly through your ICM interface.
In ICM, ruby scripts can be run through the user interface and also separately from licenced InfoWorks ICM Exchange products. ICM Exchange products do not have a graphical interface and run from a command line.
In this article, we’re not going to explain the scripts in any detail. We’ll just show them and you can follow along.
Step 1: Download an Editor.
To write scripts, you need an editor. A simple notepad like MS Notepad (but not MS Notepad) for example. This allows you to save documents as a Ruby file, using the .rb file type extension. There are many editors as a Google will show, but this article uses Notepad++. It can be downloaded from here.
Step 2: Write your Ruby Script.
Open up your editor and start typing your ruby script. Type in the text below and save off as Script1.rb. You need to save ruby scripts as .rb files.
#anything which comes after the '#'sign is a comment puts 'Hello World'
This simple script is telling Ruby to Output (display) the phrase ‘Hello World’.
Step 3: Run your Script in ICM
Select Network > Run Ruby script...
And navigate to where you saved your script
The script should run and you should see ‘Hello world’ displayed on your ICM screen. You can now run ruby scripts in ICM. Well done you.
Script 2 – Creating a Variable
myfavshow = 'Curb your Enthusiasm' puts myfavshow
In this script we’ve created a variable called myfavshow. We’ve assigned the value Curb your Enthusiasm to that variable. We’ve then told Ruby to output the value of that variable. Note, variables start with a lower case letter and contain no space. ‘Curb your Enthusiasm’ is referred to as a string (and also a TV show staring Larry David).
When you run the script in ICM you should see this output:
Script 3 – Using a Method
any_string = 'PaulNewman' puts any_string.length
In this script we create a variable called any_string and assign the value PaulNewman to it. We then apply a method called length to the variable any_string and we output it.
A method can be defined as “an expressions that returns a value” or even simpler; text which will do something in Ruby. In this case length is a method which counts the characters or the variable it is applied to. To apply a method, a ‘.’ must precede it. A . must precede any / all methods.
When you run this script you should see this output.
Script 4 – Creating an Array and Applying a Method to it.
caleywater =['paul', 'ross', 'teri', 'nico', 'giovanni', 'michael'] puts caleywater puts caleywater.length puts caleywater puts caleywater[-1]
In this script we create an array called caleywater and fill that array with 6 names. In the first line we tell Ruby to output that array (the contents of the array). In the next line we use the method length to count the number of items in the array and output that. In the next line we tell Ruby to output the item at position 0 (the first item) in the array. In the final line we tell Ruby to output the item in position -1, which is the one before the first item (so the last item).
Running the script should provide this output:
So that’s three very simple scripts. In the next articles on Ruby Scripts in ICM, we will use Ruby to interrogate an ICM sewer model. Have fun.