- We are discussing Ruby 1.9 on the Windows platform. This course is appropriate for Linux/Mac users as well.
- Ruby is an interpreted language
- In Ruby, there's always more than one way to solve a given problem.
- 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).
- By convention, Ruby source files have the .rb file extension. In Microsoft Windows, Ruby source files sometimes end with .rbw, as in myscript.rbw.
- In Ruby, program execution proceeds in general from top to bottom.
- Features: Free format, Case sensitive, Two type of comments, Statement delimiters are not required, We have around 41 Keywords.
- 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.
- We shall be referring to the documentation here.
- 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.
- Parentheses are usually optional with a method call. These calls are all valid:
foobar(a, b, c)
foobar a, b, c
- 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).
- 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.
- Some very common Ruby operators:+ addition; - subtraction; * multiplication; / division
- The increment and decrement operators (++ and --) are not available in Ruby, neither in "pre" nor "post" forms.
- Anything inside brackets is calculated first (or, more technically, given higher precedence).
- Observe how the modulus operator (%) works in Ruby.
- When you do arithmetic with integers, you'll get integer answers.
- String literals are sequences of characters between single or double quotation marks.
- In Ruby, strings are mutable. They can expand as needed, without using much time and memory.
- String concatenation is joining of two strings, using the + operator.
- The operator << is used to append to a string
- Escape sequence is the \ character. Examples: \", \\, \n
- '' (single quotes) or "" is an empty string.
- 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.
- Constants begin with capital letters. Example PI, Length
- 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.
- Use whitespace around the assignment operator:
foo = 1
- Use one initialization per line:
level = 0
size = 0
is preferred over:
level = size = 0
- 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.
- 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
- .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 rubylearning.com and in the course at rubylearning.org is drawn primarily from the Programming Ruby book, available from The Pragmatic Bookshelf.