Ruby Summary

  1. We are discussing Ruby 1.9 on the Windows platform. This course is appropriate for Linux/Mac users as well.
  2. Ruby is an interpreted language
  3. In Ruby, there's always more than one way to solve a given problem.
  4. Code layout is pretty much up to you; indentation is not significant (but using two-character indentation will make you friends in the community if you plan on distributing your code).
  5. By convention, Ruby source files have the .rb file extension. In Microsoft Windows, Ruby source files sometimes end with .rbw, as in myscript.rbw.
  6. In Ruby, program execution proceeds in general from top to bottom.
  7. Features: Free format, Case sensitive, Two type of comments, Statement delimiters are not required, We have around 41 Keywords.
  8. You may be used to thinking that a false value may be represented as a zero, a null string, a null character, or various other things. But in Ruby, all of these *values* are true; in fact, everything is true except the reserved words false and nil.
  9. We shall be referring to the documentation here.
  10. puts (s in puts stands for string; puts really means put string) simply writes onto the screen whatever comes after it, but then it also automatically goes to the next line.
  11. Parentheses are usually optional with a method call. These calls are all valid:
    foobar(a, b, c)
    foobar a, b, c
  12. In Ruby, numbers without decimal points are called integers, and numbers with decimal points are usually called floating-point numbers or, more simply, floats (you must place at least one digit before the decimal point).
  13. Note: The Fixnum and Bignum classes represent integers of differing sizes. Both classes descend from Integer (and therefore Numeric). Ruby is able to deal with extremely large numbers, and unlike many other programming languages, there are no inconvenient limits. Ruby does this with different classes, one called Fixnum (default) that represents easily managed smaller numbers, and another, aptly called Bignum, that represents "big" numbers Ruby needs to manage internally. Ruby will handle Bignums and Fixnums for you, and you can perform arithmetic and other operations without any problems. Results might vary depending on your system's architecture, but as these changes are handled entirely by Ruby, there's no need to worry.
  14. Some very common Ruby operators:+ addition; - subtraction; * multiplication; / division
  15. The increment and decrement operators (++ and --) are not available in Ruby, neither in "pre" nor "post" forms.
  16. Anything inside brackets is calculated first (or, more technically, given higher precedence).
  17. Observe how the modulus operator (%) works in Ruby.
  18. When you do arithmetic with integers, you'll get integer answers.
  19. String literals are sequences of characters between single or double quotation marks.
  20. In Ruby, strings are mutable. They can expand as needed, without using much time and memory.
  21. String concatenation is joining of two strings, using the + operator.
  22. The operator << is used to append to a string
  23. Escape sequence is the \ character. Examples: \", \\, \n
  24. '' (single quotes) or "" is an empty string.
  25. If you get a compilation error like - #<TypeError: cannot convert Fixnum into String> it means that you cannot really add a number to a string, or multiply a string by another string.
  26. Constants begin with capital letters. Example PI, Length
  27. A local variable springs into existence as soon as the interpreter sees an assignment to that variable. It is a good practice to assign nil to a local variable initially. A runtime error will be generated if the local variable is used without being assigned a value.
  28. Use whitespace around the assignment operator:
    foo = 1


  29. Use one initialization per line:

    level = 0
    size = 0

    is preferred over:

    level = size = 0
  30. x, y = y, x will interchange the values of x and y. Parallel assignment is any assignment expression that has more than one lvalue, more than one rvalue, or both. Multiple lvalues and multiple rvalues are separated from each other with commas.
  31. Local variables must start with either a lowercase letter or the underscore character (_), and they must consist entirely of letters, numbers, and underscores. Examples: india, _usa, some_var
  32. .to_i, .to_f, .to_s are used to convert to an integer, float, string respectively.

Note: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various Ruby Gurus), my acknowledgment and thanks to all of them. Much of the material on and in the course at is drawn primarily from the Programming Ruby book, available from The Pragmatic Bookshelf.