Skip to content

Instantly share code, notes, and snippets.

View thatrubylove's full-sized avatar

Jim OKelly thatrubylove

View GitHub Profile
@thatrubylove
thatrubylove / example-10.rb
Created April 17, 2014 20:33
an-exercise-in-refactoring-large-methods-in-ruby-2-example-10
module UserPasswordHashCleaner
extend self
def call(params)
return {} if params.nil?
return params if hash_has_password_info?(params)
strip_password_keys_from_hash(params)
end
protected
@thatrubylove
thatrubylove / example-11.txt
Created April 17, 2014 20:35
an-exercise-in-refactoring-large-methods-in-ruby-2-example-11
ruby user_password_hash_cleaner.rb
....
Finished tests in 0.001046s, 3824.0918 tests/s, 3824.0918 assertions/s.
@thatrubylove
thatrubylove / example-1.rb
Created April 17, 2014 21:57
a-deeper-look-at-rubys-enumerable-example-1
numbers = [1,3,5,8,10,54,99]
cards = [5,3,4,6,2]
# get only the values where the distance is greater than 10
numbers.each_cons(2).select {|a,b| b-a>10 } #=> [[10, 54], [54, 99]]
# determine if the hand is a straight
cards.sort.each_cons(5).all? do |series|
(series.first..series.last).to_a == series
end #=> true
@thatrubylove
thatrubylove / example-2.rb
Created April 17, 2014 21:58
a-deeper-look-at-rubys-enumerable-example-2
[1,3]
[3,5]
[5,8]
...
[54,99]
@thatrubylove
thatrubylove / example-3.rb
Created April 17, 2014 21:59
a-deeper-look-at-rubys-enumerable-example-2
class Roll < Array
def small_straight?
sort.each_cons(4).any? {|vals| in_series?(vals) }
end
def large_straight?
sort.each_cons(5).all? {|vals| in_series?(vals) }
end
@thatrubylove
thatrubylove / example-4.rb
Created April 17, 2014 22:00
a-deeper-look-at-rubys-enumerable-example-4
# American good grades
grades = [:A, :A, :B, :B, :C]
# Tiger momma standards
grades.all? {|grade| [:A, :B].include? grade } #=> false
# Next semester...
grades = [:A, :A, :A, :B, :B]
grades.all? {|grade| [:A, :B].include? grade } #=> true
@thatrubylove
thatrubylove / example-5.rb
Created April 17, 2014 22:01
a-deeper-look-at-rubys-enumerable-example-5
f.each_line.each_cons(2) {|line_1, line_2| line_1 == line_2 }
@thatrubylove
thatrubylove / example-1.rb
Created April 17, 2014 22:05
an-exercise-in-refactoring-large-methods-in-ruby-example-1
def update
if params[:credit_card]
#process order
response = @order.process(params)
if response[:status] == "success"
render :template => "orders/show"
else
render :json => response
end
else
@thatrubylove
thatrubylove / example-2
Created April 17, 2014 22:05
an-exercise-in-refactoring-large-methods-in-ruby-example-2
def update
render if params[:credit_card]
process_order(params)
else
create_shipping_contact(params)
end
end
def process_order(params)
response = @order.process(params)
@thatrubylove
thatrubylove / example-3.rb
Created April 17, 2014 22:06
an-exercise-in-refactoring-large-methods-in-ruby-example-3
def update
render params[:credit_card].present? ? process_order(params) :
create_shipping_contact(params)
end
def process_order(params)
response = @order.process(params)
response[:status] == "success" ? { template: "orders/show" } :
{ json: response }
end