open file mode

The file open modes are not really specific to ruby – they are part of IEEE Std 1003.1 (Single UNIX Specification). You can read more about it here:

r or rb
    Open file for reading.
w or wb
    Truncate to zero length or create file for writing.
a or ab
    Append; open or create file for writing at end-of-file.
r+ or rb+ or r+b
    Open file for update (reading and writing).
w+ or wb+ or w+b
    Truncate to zero length or create file for update.
a+ or ab+ or a+b
    Append; open or create file for update, writing at end-of-file.

marshal — serilization in ruby

define your own way to marshal

for example

calling Marshal.dump(“name”, “v”, “data”)) will call the marshal_dump method in the Myobj class

class MyObj
  def initialize name, version, data
    @name    = name
    @version = version
    @data    = data

  def marshal_dump
    [@name, @version]

  def marshal_load array
    @name, @version = array

symbol vs. string

symbol vs. string

as hash keys, string as keys is duped and  frozen so it cannot be modified. Keys to be frozen is good, but dup may be expensive., value) → value

Element Assignment—Associates the value given by value with the key given by key. key should not have its value changed while it is in use as a key (a String passed as a key will be duplicated and frozen).

determine if a constant is defined

  1. use the Ruby built-in: defined?  It works for constants, methods, classes, etc.
  2. self.class.const_defined?(:My_Constant)

There is no better among these two choices. Choosing which one depends on the user case. The second way specifies the scope to check if the constant exists, while the first way will seek for this constant through the ancestor trees. If you defines two constants with the same name but different values, you’d better use the 2nd way.


  • scope of constants:Ruby searches for the constant definition in this order:
    1. The enclosing scope
    2. Any outer scopes (repeat until top level is reached)
    3. Included modules
    4. Superclass(es)
    5. Object
    6. Kernel

better read:

class C
  X = 1
  module M
    X = 2
    class D
      X = 3
      puts X          # => 3
      puts C::X       # => 1
      puts C::M::X    # => 2
      puts M::X       # => 2    

super in ruby

The “super” keyword in ruby behaves a little differently than other languages. Instead of returning an instance of the superclass, it checks all the way up the ancestry tree to find the inherited method.

def foo(a)

Is the same as

void foo(a)

  • supper is a keyword, not a method; Methods can be overridden in subclasses of ruby.

  • ancestor tree: An ancestor of a class is either the class itself, a module included by any of the class’s ancestors or the superclass of any of the class’s ancestors. In short the ancestors of a class are all the classes and modules in which ruby will look for the method definition when you call a method on an object of that class.

module Vehicular
  def move_forward(n)
    @position += n

class Vehicle
  include Vehicular  # Adds Vehicular to the lookup path

class Car < Vehicle
  def move_forward(n)
    puts "Vrooom!"
    super            # Calls Vehicular#move_forward
puts Car.ancestors.inspect

# Output
# [Car, Vehicle, Vehicular, Object, Kernel, BasicObject]

Note the inclusion of the Vehicular Module object!