0
Digg me

This is the first part of a quick and easy 5-part tutorial for the Ruby programming language. I initially sent it to Rubidius, the local Ruby User Group I founded, as well as to my Local Linux User Group. It’s free as in free beer. Learn Ruby in one hour. At least the basics. Seriously, it will take you about an hour for the whole. Enjoy.

———————————————————————-

One-hour Ruby
An introduction to the Ruby language in 5 easy pieces

by

Josh Nursing

Introduction

This is a very brief introduction to some of Ruby’s syntax. This tutorial is designed to get you up and running with Ruby and writing scripts as rapidly as possible. It is not intended as a complete Ruby course nor as a course for Object-oriented principles. You will find it especially useful if you already know programming in some language other than Ruby and want to see how it is to program in Ruby. And it only takes an hour of your spare time.

Part 1: Variables

If you haven’t yet installed Ruby and would like to try it, it is readily available for Linux, Mac, and Windows:

http://www.ruby-lang.org/en/20020102.html

A nice install for Windows is Curt Hibbs’ one-click installer. It comes with a good IDE called FreeRIDE, but for learning, I recommend RDE as it contains Autocomplete and shows you the methods of a class, etc…

Let’s see briefly how variable are declared in Ruby.

In Ruby, part of the syntax denotes the type of locality of a variable.

1. A global variable starts with a $, like in $MaxItems
2. A local variable starts with lowercase, like in myval

Similarly, constants are defined in Ruby by using a name which starts with an UPPERCASE, like PI = 3.141592653 or Pi = 3.141592653.

Note here that we never had to define the actual type of variable. It isn’t necessary for us to define that $MaxItems is an integer. Ruby will infer this automatically. This saves a lot of time and makes you more productive.

Now, let’s see the cases of variables when you are writing classes and using objects (instances of classes).

1. A class variable starts with @@, like in @@name
2. An object instance variable starts with @, like in @address

Don’t worry about classes and objects right now as we’ll see them again in the last section.

Part 2

  • Share/Bookmark
 
blog comments powered by Disqus