infuerno.github.io

Ruby Koans

Reference: http://rubykoans.com/

Misc

Assert

Nil

Reference: http://lukaszwrobel.pl/blog/ruby-is-nil

nil is an object, it is a singleton of the class NilClass

Theoretically, nil values should be incomparable, just like it is in SQL. But for practical reasons and to spare memory, nil object was made a singleton. That is, there is always exactly one instance of the NilClass which can be simply referred to by typing nil.

is_a?(class) is an instance method which returns true if class is the class of obj or a superclass of obj

A begin / rescue / end code block will catch an exception and allow you to deal with it

begin
  nil.some_method_nil_doesnt_know_about
rescue Exception => ex
  assert_equal NoMethodError, ex.class
  assert_match(/undefined method/, ex.message)
end

To test if an object is nil call nil? e.g. if myobject.nil? ...

TODO: why is it better to do this than test for myobject == nil ?

Objects

Arrays

Arrays and assignment

Hashes

Strings

TODO - why do Ruby programmers favour << when building up strings?

Symbols

TODO: Why is it not a good idea to dynamically create a lot of symbols?

Regular expressions

TODO: When would * fail to match?

Methods

Constants

Control statements