Ruby Summary

  1. An Array is just a list of items in order. Every slot in the list acts like a variable: you can see what object a particular slot points to, and you can make it point to a different object. You can make an array by using square brackets.
  2. Arrays are indexed by integers and the index starts from 0.
  3. A trailing comma in an array declaration is ignored.
  4. You can access an array beyond its boundary limits; it will return nil.
  5. We can add more elements to an existing array.
  6. Refer to the Array documentation for a list of methods.
  7. The method each of an Array calls a block once for each element in Array, passing that element as a parameter.
  8. The variable inside the "goalposts" (i.e. | |) refers to each item in the array as it goes through the loop. You can give this any name you want.
  9. Sequences have a start point, an end point, and a way to produce successive values in the sequence. In Ruby, these sequences are created using the ".." and "..." range operators and are called Range.
  10. The two dot form creates an inclusive range, and the three-dot form creates a range that excludes the specified high value.
  11. In Ruby, the sequence 1..100000 is held as a Range object containing references to two Fixnum objects.
  12. The .to_a method converts a Range to an Array.
  13. Another use of the versatile range is as an interval test: seeing if some value falls within the interval represented by the range. We do this using ===, the case equality operator.
  14. Ranges are not limited to integers or numbers. The beginning and end of a range may be any Ruby object.
  15. A symbol looks like a variable name but it's prefixed with a colon.
  16. You can think of :id as meaning the name of the variable id, and plain id as meaning the value of the variable.
  17. Symbols are useful because a given symbol name refers to the same object throughout a Ruby program.
  18. Symbols can be considered constants without values.
  19. Symbols are more efficient than strings. Two strings with the same contents are two different objects, but for any given name there is only one Symbol object. This can save both time and memory.
  20. When do we use a string versus a symbol?
    • If the contents (the sequence of characters) of the object are important, use a string.
    • If the identity of the object is important, use a symbol.
  21. A Symbol object is created by prefixing an operator, string, variable, constant, method, class, module name with a colon.
  22. If Fred is a constant in one context, a method in another, and a class in a third, the Symbol :Fred will be the same object in all three contexts.
  23. Hashes are similar to arrays in that they are indexed collection of object references. However, while you index arrays with integers, you can index a hash with objects of any types: strings, regular expressions, and so on.
  24. When you store a value in a hash, you actually supply two objects - the index (normally called the key) and the value.
  25. nil is returned when an attempt is made to access keys that do not exist in the hash.
  26. The method to get a randomly chosen number in Ruby is rand.
  27. If you call rand, you'll get a float greater than or equal to 0.0 and less than 1.0. If you give it an integer parameter (by calling rand(5) ), you will get an integer value greater than or equal to 0 and less than 5.
  28. The method can open a file in different modes like 'r' Read-only, starts at beginning of file (default); 'r+' Read/Write, starts at beginning of file; 'w' Write-only, truncates existing file to zero length or creates a new file for writing.
  29. opens a new File if there is no associated block. If the optional block is given, it will be passed file as an argument, and the file will automatically be closed when the block terminates.
  30. Always close a file that you open. In the case of a file open for writing, this is very important and can actually prevent lost data.
  31. The seek method of class IO, seeks to a given offset an Integer (first parameter of method) in the stream according to the value of second parameter in the method. The second parameter can be IO::SEEK_CUR - Seeks to first integer number parameter plus current position; IO::SEEK_END - Seeks to first integer number parameter plus end of stream (you probably want a negative value for first integer number parameter); IO::SEEK_SET - Seeks to the absolute location given by first integer number parameter.

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.