This log highlights some interested points while reviewing Ruby syntax and OOP (Object-oriented Programming).

Abstraction ensures that as users, we’re far removed from what’s happening “under the hood”.1

Fundamental Ruby

Hashes

Hashes as optional parameters (Ruby does not support optional arguments),

def greeting(name, options={})
  if options.empty?
    puts name
  else
    puts "#{name} #{options[:age]} #{city}"
  end
end

greeting("Xinyang", age: 13, city: "Xian")

Files

The flow for working with files is open-use-close, otherwise, the open files will continue to occupy space in memory.

Variables as Pointers

Variables are pointers to physical space in memory in Ruby.2

Imgur

Imgur

Blocks and Procs

& indicates that the argument is a block, which must be the last parameter in the method definition.

def take_block(number, &block)
  block.call(number)
end

take_block(13) { |num| puts "Block being called in the method #{num}" }

Procs are blocks that are wrapped in a proc object and stored in a variable, which provide flexibility to be able to reuse blocks in more than one place.

proc = Proc.new { |num| puts "Block in the proc #{num}" }
proc.call(13)

Exception

Exception Handling

Exception handling deals with errors in a manageable and predictable way. Program will stop once it hit the exception if there is no rescue block.

begin, rescue, end are used to signify exception handling.

begin
  # perform dangerous operation
rescue
  # do this if operation fails
end

Stack Traces

Stack trace provide a detailed trace that shows the flow of execution and where the error occurred. Blow is a sample Ruby stack trace,

greeting.rb:2:in 'space_out_letters': undefined method 'split' for 1:Fixnum (NoMethodError)
from greeting.rb:6:in 'greet' from greeting.rb:10:in 'decorate_greeting'
from greeting.rb:14:in '<main>'

Fundamental OOP

OOP is a programming paradigm that was created to deal with the growing complexity of large software systems.3

  • Encapsulation is hiding pieces of functionality and making it unavailable to the rest of the code base.
  • Polymorphism is the ability for data to be represented as many different types.
  • Inheritance is used in Ruby where a class inherits the behaviors of another class

Module, a collection of behaviors that is reusable in other classes via mixins in Ruby.

Class & Objects

Class defines states and behaviors. Class variable are created using @@. self can be used to define class method (see below).

class Person
  @@id = 0
  def self.get_id
    @@id
  end
end

When to use self.foo inside a class,4

  1. Always omit self. when invoking methods
  2. Use self.foo = xxx when calling a setter method (Ruby know you are not creating new variable)
  3. You cannot use self.foo() to call a private method (Must callfoo())

Inheritance

super call methods up the inheritance hierarchy, it search the inheritance hierarchy for method by the same name and then invoke it.

class Animal
  attr_accessor :name
  def initialize(name)
    @name = name
  end
end

class GoodDog < Animal
  def initialize(name, color)
    super(name)
    @color = color
  end
end

bruno = GoodDog.new("Guai-guai", "brown")

Inheritance and mixing in modules are two ways to implement Inheritance in Ruby.

  • Ruby only supports single inheritance, a class can have multiple mix-ins
  • Inheritance for “is-a” relationship, modules for “has-a” relationship
  • Modules can only used for namespacing and grouping methods

Module

Mixing in Modules

The order in which modules include in a class is important. Ruby looks at the last module included in a class first.

More About Modules

Using modules for namespacing,

module Mammal
  class Dog
    def speak(sound)
      p "#{sound}"
    end
  end
end
guai_guai = Mammal::Dog.new

Using modules as a container,

module Mammal
  def self.some_out_of_place_method(num)
    num ** 2
  end
end

value = Mammal.some_out_of_place_method(3)
# Alternatively
value = Mammal::some_out_of_place_method(3)
  1. https://launchschool.com/books/ruby/read/introduction

  2. https://launchschool.com/books/ruby/read/more_stuff

  3. https://launchschool.com/books/oo_ruby/read/the_object_model

  4. http://stackoverflow.com/questions/4699687/when-to-use-self-foo-instead-of-foo-in-ruby-methods