Skip to content

Instantly share code, notes, and snippets.

View marka2g's full-sized avatar
🎯
Focusing

Mark Sadegi marka2g

🎯
Focusing
View GitHub Profile
@marka2g
marka2g / ruby_bits_solutions_procs_1.rb
Created March 18, 2013 02:08
ruby_bits_solutions_1_procs
#ruby bits - solutions - procs
class Game
attr_accessor :name, :year, :system
def initialize(name, options = {})
@name = name
@year = options[:year]
@system = options[:system]
end
end
@marka2g
marka2g / proc_to_lambda.rb
Created March 18, 2013 02:11
refactor a proc to lambda
# refactor this Proc def to use lambda
library = Library.new(GAMES)
print_details = Proc.new do |game|
puts "#{game.name} (#{game.system}) - #{game.year}"
end
library.exec_game('Contra', print_details)
# lambda
library = Library.new(GAMES)
@marka2g
marka2g / ruby_bits2_solution_multiple_lambdas.rb
Created March 18, 2013 02:28
ruby bits 2 solution 3 - multiple lambdas
# Refactor exec_game to take a second Proc argument for error handling purposes.
# If calling the action Proc raises an exeption, call the new error Proc.
# We've already modified the code in main.rb to create the error Proc and pass it to exec_game.
# Hint: You'll want to add a begin/rescue/end block around action.call.
class Game
attr_accessor :name, :year, :system
def initialize(name, options = {})
@name = name
@year = options[:year]
@marka2g
marka2g / ruby bits2_solution4_proc_to_block.rb
Created March 18, 2013 02:32
ruby bits 2- solution 4 - proc to block
#ruby bits 2- solution 4 - proc to block
# Move the code in the each block into a Proc, and then pass that Proc into library.each.
class Game
attr_accessor :name, :year, :system
def initialize(name, options = {})
@name = name
@year = options[:year]
@system = options[:system]
end
@marka2g
marka2g / super.rb
Created March 18, 2013 02:37
using super - simple
# using super
class Parent
def initialize(name)
puts 'parent_' + name
end
end
class Child < Parent
@marka2g
marka2g / capturing_a_block_rb2_solution5.rb
Created March 18, 2013 02:41
capturing a block - rb2 - solution 5
#ruby bits 2- solution 5 - capturing block
# Refactor this each method to capture its block as a Proc object, then inside of the games.each
# block, call the captured Proc instead of yielding.
# OLD
# class Library
# attr_accessor :games
# def initialize(games)
# @games = games
@marka2g
marka2g / rb2_solution6_passing_blocks.rb
Created March 18, 2013 02:48
rb2 solution7 - passing blocks
# Refactor the each method to be just one line of code, passing the captured block directly to games.each.
# OLD
# class Library
# attr_accessor :games
# def initialize(games)
# @games = games
# end
@marka2g
marka2g / rb2_solution8_symbol_to_proc.rb
Created March 18, 2013 02:50
rb2 solution7 - symbol to proc
# symbol to proc
# Refactor the games.map call, removing its block and using Symbol#to_proc instead.
# OLD
# class Library
# attr_accessor :games
# def initialize(games)
# @games = games
# end
@marka2g
marka2g / rb2_solution8_optional_blocks.rb
Created March 18, 2013 02:59
rb2_solution8_optional_blocks
# Optional Blocks
# Implement this list method that takes an optional block. If a block is passed, yield each game to the block
# and print the result. If there is no block, print each game's name.
# OLD
# class Library
# attr_accessor :games
# def initialize(games)
# @games = games
@marka2g
marka2g / short_circuit_evaluation.rb
Created March 20, 2013 11:06
short circuit evaluation ||
# short circuit evaluation - ||
def sign_in
current_session || sign_user_in
end