Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Converting to Ruby? Start here

WHAT IS RUBY?

It's an open source programming language.

Why do we need another programming language? Well - perhaps we don't actually need one, but ruby's 'metrics' are a bit different to other languages so it fits well into places others have to be kludged, and it's not got a long history to which all new releases must remain faithful / compatible.

SOME KEY CHARACTERISTICS

According to the Ruby web site (but re-written by myself to show you the benefits as well as the features):

Ruby has simple syntax. Easy to learn and, fast to interpret.

Ruby has exception handling to make it easy to handle errors.

Ruby's operators are syntax sugar for the methods and you can redefine them.

Ruby is a complete, full, pure object oriented language: OOL. All data in Ruby is an object. This keeps it very clean and gives great flexibility.

Ruby's OO is carefully designed to be both complete and open for improvements. Example: Ruby has the ability to add methods to a class, or even to an instance during runtime. So, if needed, an instance of one class *can* behave differently from other instances of the same class.

Ruby features single inheritance only, intentionally to avoid excess complexity and poor design. However,Ruby knows the concept of modules (collections of methods) which classes can import and get all its methods for free. Multiple inheritance (the alternative) is a nightmare well avoided.

Ruby features blocks in its syntax (code surrounded by '{' ... '}' or 'do' ... 'end'). These blocks can be passed to methods, or converted into closures.

Ruby features a true mark-and-sweep garbage collector. Efficient reuse of memory and dynamic allocation of memory only when you need it.

Writing C extensions in Ruby is easier than in other languages.

Integers in Ruby can (and should) be used without counting their internal representation. No overflow problems.

Ruby needs no variable declarations. It uses simple naming conventions to denote the scope of variables. Examples: simple 'var' = local variable, '@var' = instance variable, '$var' = global variable.

Ruby can load extension libraries dynamically if an OS allows.

Ruby features OS independent threading.

Ruby is highly portable; it works on Linux, most UNIX, Windows, MacOS, DOS, BeOS, OS/2, etc.

See: http://www2.ruby-lang.org/en/20020101.html

LAYOUT OF STATEMENTS

Line end is end of statement

Where a statement is clearly not completed on one line, it is carried on to another.

A ; can force a new statement

VARIABLE SUMMARY:

Case sensitive names.

No typing - everything is an object

"Bare" words - at least for local variables - no leading $ @ % or anything else

Variables appear automatically, but not assumed to start at 0 or empty - they must be initially assigned.

COMMENTS

# through to line end. Applies from start of line of from within a line where # is a new token in the base context.

INTERACTIVE TESTING

use irb - interactive ruby, supplied with the distribution

earth-wind-and-fire:~/ruby/r101 grahamellis$ irb
irb(main):001:0> 5
=> 5
irb(main):002:0> y = 6
=> 6
irb(main):003:0> y * 7
=> 42
irb(main):004:0> exit
earth-wind-and-fire:~/ruby/r101 grahamellis$

SAMPLE PROGRAMS

So much for the theory and introduction - let's see a sample program:

# Single quote - literal string
print 'Hello World\n'

# Multiple quote - \ translation
print "Hello World\n"

# Gets to read from keyboard
print "Please enter your name: "
# assignment creates a variable; gets is an operator?
stuff = gets

# chomp to strip off last character - RETURNS a string
lessstuff = stuff.chomp

print lessstuff," and fries\n"

How does that run?

earth-wind-and-fire:~/ruby/r101 grahamellis$ ruby hello.rb
Hello World\nHello World
Please enter your name: Graham
Graham and fries
earth-wind-and-fire:~/ruby/r101 grahamellis$

There's clearly a lot more to explain there, but you'll see the basis of an easy, short language where you can write a lot of effective and short code quickly ... with the important bonus that the code is almost 'naturally readable' even to newcomers to the language.

Here are some terms you'll come across time and again with Ruby that we haven't mentioned yet:

PRAGMATIC

You'll come across references to "pragmatic" programming. The following are definitions of pragmatic in general (non-programming) use:

1. Dealing or concerned with facts or actual occurrences; practical.

2. Relating to or being the study of cause and effect in historical or political events with emphasis on the practical lessons to be learned from them.

What does that mean in programming terms - to quote David Thomas and Andrew Hunt, Authors of "Programming Ruby, the Pragmatic Programmer's Guide",

Our job is to solve problems, not spoon-feed compilers, so we like dynamic languages that adapt to us, without arbitrary, rigid rules. We need clarity so we can communicate using our code. We value conciseness and the ability to express a requirement in code accurately and efficiently. The less code we write, the less that can go wrong. (And our wrists and fingers are thankful, too.)

We want to be as productive as possible, so we want our code to run the first time; time spent in the debugger is time stolen from the development clock. It also helps if we can try out code as we edit it; if you have to wait for a 2-hour make cycle, you may as well be using punch cards and submitting your work for batch compilation.

We want a language that works at a high level of abstraction. The higher level the language, the less time we spend translating our requirements into code.

See: http://www.ruby-doc.org/docs/ProgrammingRuby/

ROR - RUBY ON RAILS

"Ruby on Rails" is a framework for web based, database driven applications. In a nutshell, it provides the programmer with a very effective tool with which to provide the functionality needed by web users (and site and data administrators too) in accessing, searching and modifying information stored in SQL tables. As you would expect from the name, the programatic elements of RoR are written in Ruby, and if you're going to be a Ruby on Rails developer, you'll want to start off by learning something of the Ruby language.

SOUNDS GREAT - WHERE NOW?

This page is taken from our 2 day Ruby programming course that you can book through this site. And if you're wanting to browse further material but aren't serious enough about it to have the justification for a course, you'll find plenty of other material on this site and on links elsewhere.


See also Ruby course - UK, Ireland, Europe

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Ruby - Control Structures
  [4674] Alternating valuses / flip-flop / toggle - example in Ruby - (2016-05-17)
  [4504] Where does Ruby load modules from, and how to load from current directory - (2015-06-03)
  [4503] Separating your code for easier testing, understanding and re-use; example in Ruby - (2015-06-02)
  [4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
  [4370] Conditionals, loops and methods in Ruby - a primer with simple examples - (2014-12-29)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [3769] Muttable v immutable and implications - Ruby - (2012-06-20)
  [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
  [3619] Ruby v Perl - a comparison example - (2012-02-21)
  [3422] Assigning values to variables within other statements - Ruby - (2011-09-07)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3254] Multiple inputs, multiple out, ruby functions - (2011-04-19)
  [3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
  [3158] Ruby training - some fresh examples for string handling applications - (2011-02-05)
  [3156] Splitting data reading code from data processing code - Ruby - (2011-02-04)
  [2975] Why do I need brackets in Ruby ... or Perl, Python, C or Java - (2010-09-29)
  [2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
  [2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
  [2619] Passing code to procedures and yield in Ruby - (2010-02-02)
  [2471] A short form of if ... then ... else - (2009-10-23)
  [2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
  [1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
  [1891] Ruby to access web services - (2008-11-16)
  [1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
  [1870] What to do with a huge crop of apples - (2008-11-04)
  [1738] Clean code, jump free (Example in Lua) - (2008-08-06)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1587] Some Ruby programming examples from our course - (2008-03-21)
  [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [995] Ruby's case - no break - (2006-12-17)
  [985] Equality in Ruby - == eql? and equal? - (2006-12-14)
  [962] Breaking a loop - Ruby and other languages - (2006-12-03)
  [960] 1st, 2nd, 3rd revisited in Ruby - (2006-12-02)

Ruby - General
  [4583] Back in the saddle again - excellent open source course from Well House Consultants - (2015-11-26)
  [4294] A bright new gem - updated Ruby training - (2014-09-16)
  [3799] Ruby Documentation through rdoc - (2012-07-07)
  [3158] Ruby training - some fresh examples for string handling applications - (2011-02-05)
  [2866] Ruby - how does it compare and where is it the right language? - (2010-07-11)
  [2826] Ruby - training for automated testing users - (2010-06-25)
  [2605] Ruby on Rails - a sample application to teach you how - (2010-01-30)
  [2504] Learning to program in ... - (2009-11-15)
  [2227] Learning PHP, Ruby, Lua and Python - upcoming courses - (2009-06-11)
  [2104] Ruby Programming and Rails - 4 different courses in one - (2009-03-26)

resource index - Ruby
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/ruby-con ... -here.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard