Skip to content

Instantly share code, notes, and snippets.

View thatrubylove's full-sized avatar

Jim OKelly thatrubylove

View GitHub Profile
@thatrubylove
thatrubylove / example-5.rb
Created April 16, 2014 05:05
a-faux-o-deck-of-cards-example-5
class Deck
SUITS = %w(hearts clubs spades diamonds)
RANKS = %w(ace two three four five six seven
eight nine ten jack queen king)
...
end
@thatrubylove
thatrubylove / example-6.rb
Created April 16, 2014 05:06
a-faux-o-deck-of-cards-example-6
class Deck
...
protected
def all_ranks
RANKS.map.with_index {|rank, value| Rank.new(rank, value) }
end
end
@thatrubylove
thatrubylove / example-7.rb
Created April 16, 2014 05:07
a-faux-o-deck-of-cards-example-7
class Deck
...
protected
def build_deck
all_ranks.flat_map {|rank| SUITS.flat_map {|suit| Card.new(rank,suit) } }
end
...
@thatrubylove
thatrubylove / example-8.rb
Created April 16, 2014 05:07
a-faux-o-deck-of-cards-example-8
Rank = Struct.new(:rank, :value) do
def to_s; rank; end
end
Card = Struct.new(:rank, :suit) do
def to_s; "#{rank} of #{suit}"; end
end
class Deck
SUITS = %w(hearts clubs spades diamonds)
@thatrubylove
thatrubylove / example-9.rb
Created April 16, 2014 05:08
a-faux-o-deck-of-cards-example-9
blackjack_deck = Deck.new(8)
poker_deck = Deck.new
@thatrubylove
thatrubylove / example-1.rb
Created April 17, 2014 20:21
an-exercise-in-refactoring-large-methods-in-ruby-2
...
def update
if (params[:user][:password].blank? && params[:user][:password_confirmation].blank?)
params[:user].delete(:password)
params[:user].delete(:password_confirmation)
end
if params[:user][:photo_attributes]
Photos::Manipulation.update_resource_photo(current_user.photo || Photo, params[:user][:photo_attributes], current_user)
current_user.photo.try(:reload)
@thatrubylove
thatrubylove / example-2.rb
Created April 17, 2014 20:22
an-exercise-in-refactoring-large-methods-in-ruby-2-example-2
if (params[:user][:password].blank? && params[:user][:password_confirmation].blank?)
params[:user].delete(:password)
params[:user].delete(:password_confirmation)
end
@thatrubylove
thatrubylove / example-3.rb
Created April 17, 2014 20:23
an-exercise-in-refactoring-large-methods-in-ruby-2-example-3
module UserPasswordHashCleaner
extend self
end
@thatrubylove
thatrubylove / example-4.rb
Created April 17, 2014 20:24
an-exercise-in-refactoring-large-methods-in-ruby-2-example-4
module UserPasswordHashCleaner
extend self
end
require 'minitest/autorun'
describe UserPasswordHashCleaner do
subject { UserPasswordHashCleaner }
describe "call(params)" do
@thatrubylove
thatrubylove / example-5.rb
Created April 17, 2014 20:25
an-exercise-in-refactoring-large-methods-in-ruby-2-example-5
module UserPasswordHashCleaner
extend self
def call(params)
return {} if params.nil?
end
end
require 'minitest/autorun'