Sing-along-time: Sugar SugarMy client is now live

# Ruby Basics

technological @ 04 March 2007

Ruby is a language, which, you will probably end up learning to love.

It is an Object-Oriented Language, which means that everything within Ruby is an object. An Object has properties (e.g. a watch is X heigh, Y width and Z depth etc) and it has methods (e.g. set an alarm, illuminate etc).

Ruby also takes some ideas from the Functional Programming Model. Functional Programming is a mathematical way to describe what a program should do (well known Functional Languages include Haskell, Miranda, ML and LISP). Ruby has limited functional support, but it is there none the less. I will not go into much detail about this, the best thing you could do if you want to know about this is look up topics such as Lambda Calculus, Closures and Blocks.

Finally, Ruby is also (typically) an interpreted language. The official version of Ruby and the Ruby libraries comes with a Ruby Interpreter (ruby on the command line), it also comes with the Interactive Ruby Shell (irb on the command line).

In true programming language tradition we start with the Hello World program. In Ruby this is nice and simple:

puts "Hello, World!"

That it, that’s all it is. Lets analyse it a bit:

puts — this is a system level function which prints text to screen.
"Hello, World!" — this is an instance of the Ruby String class.

We can also put a number to screen:

puts 3

A little analysis shows that 3 is automatically assigned to the number class (more specifically Fixnum).

So lets now go on to outputting a number to screen and concatenate it with a string. You would imagine, it would look something like this:

puts 3 + "Is the magic number"

However we get this error:
TypeError: String can't be coerced into Fixnum
from (irb):5:in `+'
from (irb):5

This is a nasty looking error, but its basically saying that you can use the concatenate operator with different types (in this case a number type and a string type). This is because although Ruby is a dynamic typed language (a language that can assign types to variables when they are used), it is a strictly typed language (a language that has types that have rules).

As you can see, in Ruby types are important, even though you do not have to specifically say what type a particular variable is. This leads us on to variables. Variables can be assigned on the go. Using ruby we can say something like this:

magicNumber = 3
puts magicNumber

and 3 will be outputted to the screen. Simple, magicNumber becomes a Fixnum, and the ruby system knows how to show a simple number. Lets now go back to our concat example, but this time using the variable:

magicNumber = 3
puts magicNumber.to_s + " Is the magic number"

The output will be: 3 Is the magic number

In the above I have introduced a method on the Fixnum type. This method is called .to_s it turns the number into a string, which we can then use in our concat. Whenever you see the dot (.) in Ruby, it usually means that what is about to follow is a member of the class (in other words, either a property or a method of the type).

Now I have covered the very basics, I shall now cover the basics that every software developer needs.

Selection

If example:

if x == 1
puts "Hello"
end

Case example:

food = case shoppingItem
when "chips" then true
when "loo roll" then false
when "bananas" then true
when "guitar" then false
when "action figure" then false
end

Iteration

A very useful while example:

# Centigrade to Fahrenheit converter
puts "Welcome to the C to F Converter\nPlease input the lowest value to convert\n"
low = gets().to_i
puts "Please input the highest value to convert\n"
high = gets().to_i
puts "Please input the interval to convert at"
interval = gets().to_i
currentTempC = low
currentTempF = 0
while currentTempC <= high
currentTempF = ((90/5)*currentTempC) + 320
puts currentTempC.to_s + "C = " + (currentTempF/10).to_s + "." + (currentTempF%10).to_s + "F \n"
currentTempC = currentTempC + interval
end

Here are some notes for the above:
# is a comment
gets() gets a value from keyboard
to_i turns a string into an integer

A for loop example

for i in 1...20
puts "This is the loop count: " + i.to_s
end

Class definitions

class Human
# variables and accessors go in here
# method definitions go in here
end

Method definitions

def run
#put one foot in front of the next quickly, and repeat
end

Quick Summery

This has been slow introduction, with a quick run through of the basics of ruby. There are plenty of resources out there to learn about Ruby.

Resources

You can get Ruby and Ruby Documentation here: https://www.ruby-lang.org/

If you don’t fancy downloading Ruby then you can use the online Interactive Ruby Shell available here: https://tryruby.hobix.com/

More information about Functional Ruby can be found here: https://www.artima.com/intv/closures.html

I can also provide training and consultancy for a small fee if you fancy learning more: https://www.vanirsystems.com/

Technorati Tags: ruby, tutorial, basics

Leave a Reply