From Wiki
Jump to navigationJump to search


  • Focus is on the programmer rather than the machine. i.e. maximize programmer efficiency.
  • Principle of least astonishment - behave in such a way that minimized the confusion of experienced Ruby programmers.
  • Ruby is an object-oriented scripting language.
  • Multi paradigm :
    • Scripting : automate tasks within some environment
    • Imperative : traditional control structures
    • Object oriented : everything is an object
    • Functional : Also exists. Computation proceeds via the evaluation of functions that depend only on their input, not program state.

Classes and Inheritance

class MyClass
   @boo #instance variable
   def my_method
     @foo = 2 #instance variable
mc = #create a myclass object
mc.my_method     #calls the method           #error, not allowed to access variable
  • An instance variable can only be accessed or modified within a method definition.
class MyClass
 def boo  #getter method
   return @boo

 def boo=(val) #setter method
   @boo = val
mc = #create a myclass object
boo = 1 #calls the setter method
boo  #calls the getter method
  • Shortcut
class Person
   attr_accessor :first_name, :last_name #creates instance variables and getter/setter methods
  • No return values specified in the method. The value of the last expression executed in the method is its return value.
def min (x,y)
  if x < y then x else y end
  • When invoking a method, parentheses are optional.
  • Class methods (static methods) are created in the same way as normal methods, except are prefixed with keyword "self".
  • Array.methods returns all methods in the Array class.
  • Ruby convention is to use the last character of a method name to indicate its behaviour. e.g ? is used to indicate return of boolean. "!" is changing the state of the object.
  • self refers to the current object. (similar to this)
  • Only single inheritance is supported, but mixin capability offers multiple inheritance.
  • Classes are NEVER closed, always add more instance vars, methods etc. (Both built in and custom classes)
class Fixnum #built in ruby class
 def previous #add a new method
   return self -1

32.previous #returns 31


  • Within a class definition
  • public - no access control
  • protected - only be invoked by objects of the defining class and its subclasses
  • private - only be called in the context of the current object with no reference on LHS. Thus can only be used on self. Two objects of the same class cannot call each others private methods.
  • Only invoke a private method from within another method of the class.
  • By default every method is public and variables are protected.


  • class MyClass < SuperClass
  • The initialize method is always private - used to create a new constructor, can be overriden.
  • Can create "modules" like packages. include a module by using the require keyword.
  • Use include keyword to mixin a module to make it part of the class.

Objects and Variables

  • Everything in Ruby is an object.
  • class() method is in the object class. So, 1.class() will print fixnum.
  • There are no variable declarations. Just assign values to literal and use. Duck typing : type of the variable based on what it looks like.
  • ALL assignements are done by reference in Ruby. A variable holds a reference to an object and does not care about the type (since everything inherits from an Object).
  • Supports parallel assignments. a = 1, b = 2. Then we can a, b = b, a. (a assigned to be and b assigned to a, effectively swapping them)
  • Ruby uses simple naming conventions to denote the scope of the variable. (To enhance readibility)
    • name - local - lowercase
    • @name - instance variable
    • @@name - class variable (static)
    • $Name - global variable capitalized

Strings, Regex, Symbols

  • Strings either single, double quotes.
  • String interpolation can be done using double quotes (executes a Ruby expr) e.g. str = " 360 Degrees = #{2*Math::PI} radians "
  • String enclosed in backticks is executed as a command to the underlying OS.
  • Strings in Ruby are mutable. However, every time there is a new String literal, Ruby creates a new String object.
  • Regex support is in the Regexp class.
  • Written in the form of /pattern/modifiers (Perl Style)
  • To test if a regex matches a string use the =~ operator.
  • "Homer" =~ /er/ : returns 3. If no match, returns nil.
  • Replace non digits in a phone number with blanks :
phone.gsub!(/\D/, "")
  • Symbols are closely related to Strings, are non mutable.
  • Ruby interpretor maintains a symbol table - where it stores the names of all classes, methods and variables.
  • We can add our own symbols to this table, name preceded with a colon is a symbol. e.g :row, :col.
  • Used to represent names and strings, however they exist only once per ruby session & cannot be modified during runtime.
  • So they offer a space advantage.
  • Use case : if the identity of the object is important, use a symbol.

Expressions and control-structures

  • Ruby syntax is expression oriented - everything is an expression and therefore evaluates to something.
  • Control structures which are traditionally statements are treated as expressions in Ruby. (The value they assume is the last expr they evaluated)
  • Conditionals if expr code end. No curly braces. Also elsif available.
  • Shorthand : code if <expr>
  • ==, !=, =~ (regx), !~ (not regex) === (case equality)
  • Oppposite of if exists : until <expr> code end, executes code until expr is true (other than false/nil)
  • for/in loop iterates over an enumerable collection. for var in collection do body end
  • while condition do body end, until condition do body end.

Collections, blocks, iterators

  • Array, Hash, Set collections.
  • The collection classes include Enumerable module as a mixin, they share a common set of methods provided by enumerable which provides iterator methods.
  • Array can also be treated as a stack, push, pop etc.
def index 
 @post = Post.all #post variable is an Array 

a = [33, "hello", 2.2] //Non homogeneous array

a[1..2] #returns a sub array
  • Hashes : associative array for array its an int, hash key is any object.
phone = {:home => 1, :work => 2, :mobile => 3}

phone[:work] # Returns 2
phone.key(1) # Returns home
  • Multidimensional collections : nest one collection inside another
city = {"blr" => {:state => "KA"}, "mas" => {:state => "TN"}}
city["blr"][:state] #prints KA

Code blocks

  • A block consists of multiple lines of code, enclosed in {}, that can be passed to a method.
  • A block may only appear in the source if it is adjacent to the method call.
  • A block is called using the yield statement.
def three_times

three_times {puts "Hello World"}


  • Loop structures are rarely used.
  • Iterator method invokes a block of code, applying it to each element in the collection.
  • Iterators work because we can pass parameters into blocks.
a = [1,2,3]

a.each {|element| puts element + 1} # prints 2,3,4