Hack hack hack...

An open journal-- some of it written for you, but most of it is for me.

Ruby Bits 2

Multiple Lambdas
1
2
3
4
5
6
7
8
def exec_game(name, action, error_handler)
    game = games.detect { |game| game.name = name }
    begin
      action.call(game)
    rescue
      error_handler.call
    end
  end
Lambda or Proc to Block
1
2
3
library = Library.new(GAMES)
printer = lambda { |game| puts "#{game.name} (#{game.system}) - #{game.year}" }
library.each(&printer)

Calling a method with & in front of a parameter tweets.each(&printer)

Defining a method with & in front of a parameter def each(&block)

Passing blocks around…
1
2
3
4
5
6
7
8
9
10
11
  def each
    games.each do |game|
      yield game
    end
  end

#refactored as

  def each(&block)
    games.each(&block)
  end

Comments