This page contains some random notes on Ruby.

Before talking about the good stuff, here's some Dirt.

Useful Methods

a.send(:set_defaults) is exactly the same as a.set_defaults(), except that it gets to ignore public/private, etc.

  • a.inspect -- prints [a,b] instead of ab
  • a.methods.sort -- prints all the methods the object can respond to
  • a.class, a.class.superclass -- prints object hierarchy
  • a.class_variables, a.instance_variables

object_id(): returns a unique id for the object. Used to see if a reference refers to a copy of the object or to the object itself.

To pretty-print var: require 'pp'; pp var

Methods on arrays:

  •, a.reject -- produce an array containing just those items where the block returned true or false.
  • a.collect / -- Replace the array with an array of the block's return values.
  • a.uniq -- return an array with all duplicates removed

Object Model

local, $global, @instance, @@class (but try not to use @@class)


  • inside an instance method, refers to the receiver object.
  • outside an instance method but inside a class definition: the class being defined
  • anywhere else: unrecognized?
  • Class#meth <-- instance method: class C; def meth ...
  • Class.meth <-- class method: Class C; def C.meth ...


  • inside a method: call the same method, but start the search in the class's superclass
  • anywhere else: unrecognized?

Quoting Table

perl	ruby		unix	what it does
----   ----            ----    ------------
q//	%q//		''	Single quote, quotes a string with no interpolation
qq//	%Q// or %//	""	Double quote, quotes a string with interpolation (although perl and ruby interpolate differently)
qx//	%x//		``	Backticks—execute command
qw//	%w//			Quote words into a list/array
m//	%r//		//	Regular Expression
s///	String#sub		Substitution
tr///	String#tr		Translation character by character


Silly Tricks

Run upcase on each string in names: result =