string format convert

"CoolCat".underscore  #=> "cool_cat"
"cool_cat".capitalize #=> "Cool_cat"
"cool_cat".camelize   #=> "CoolCat"
Advertisements

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:http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html

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(Myobj.new(“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
  end

  def marshal_dump
    [@name, @version]
  end

  def marshal_load array
    @name, @version = array
  end
end

symbol vs. string

symbol vs. string

http://stackoverflow.com/questions/8189416/why-use-symbols-as-hash-keys-in-ruby

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.

hash.store(key, 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).

http://stackoverflow.com/questions/13044839/why-is-a-string-key-for-a-hash-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.

see http://tech.groups.yahoo.com/group/Houston-RoR/message/2926

  • 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: http://coderrr.wordpress.com/2008/03/11/constant-name-resolution-in-ruby/

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    
    end
  end
end

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)
super
end

Is the same as

void foo(a)
{
closest_ancestor_that_has_foo_method.foo(a)
}

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

http://www.rubyinair.com/2011/04/super-is-keyword-not-method-call.html

  • 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.

http://ruby-doc.org/docs/keywords/1.9/files/keywords_rb.html#M000034

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

class Vehicle
  include Vehicular  # Adds Vehicular to the lookup path
end

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

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

Note the inclusion of the Vehicular Module object!