Ruby Summary

  1. Refer to the String documentation to use the various methods available.
  2. For double-quoted string literals, Ruby looks for substitutions - sequences that start with a backslash character - and replaces them with some binary value or does expression interpolation ie. within the string, the sequence #{expression} is replaced by the value of the expression.
  3. It is to be noted that every time a string literal is used in an assignment or as a parameter, a new String object is created.
  4. Observe how one can list all the methods of a class or object.
  5. Comparing two strings for equality can be done by == or .eql? (for identical content) and .equal? (for identical objects).
  6. %w is a common usage in strings.
  7. Observe the usage of constructs: if else end, while, if elsif end
  8. Ruby also has a negated form of the if statement, the unless end.
  9. Case Expressions: This form is fairly close to a series of if statements: it lets you list a series of conditions and execute a statement corresponding to the first one that's true. case returns the value of the last expression executed. Usage: case when else end
  10. IMPORTANT: Ruby Code blocks are chunks of code between braces or between do- end that you can associate with method invocations.
  11. Code blocks may appear only in the source adjacent to a method call; the block is written starting on the same line as the method call's last parameter (or the closing parenthesis of the parameter list). The code in the block is not executed at the time it is encountered. Instead, Ruby remembers the context in which the block appears (the local variables, the current object, and so on) and then enters the method.
  12. The Ruby standard is to use braces for single-line blocks and do- end for multi-line blocks. Keep in mind that the braces syntax has a higher precedence than the do..end syntax.
  13. Inside a method, you can call a Ruby block using the yield keyword with a value.
  14. You can provide parameters to the call to yield: these will be passed to the block. Within the block, you list the names of the arguments to receive the parameters between vertical bars (|).
  15. The do and end identify a block of code that will be executed for each item.

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.