Skip to content

Instantly share code, notes, and snippets.

View thatrubylove's full-sized avatar

Jim OKelly thatrubylove

View GitHub Profile
@thatrubylove
thatrubylove / example-6.md
Created April 17, 2014 20:26
an-exercise-in-refactoring-large-methods-in-ruby-2-example-6

require 'minitest/autorun'

describe UserPasswordHashCleaner do subject { UserPasswordHashCleaner } describe "call(params)" do it "will return the hash as is if it has a password && password_confirmation" do params = { user: { password: "ok", password_confirmation: "ok", admin: true } }

@thatrubylove
thatrubylove / example-7.rb
Created April 17, 2014 20:27
an-exercise-in-refactoring-large-methods-in-ruby-2-example-7
module UserPasswordHashCleaner
extend self
def call(params)
...
return params if hash_has_password_info?(params)
end
protected
@thatrubylove
thatrubylove / example-8.rb
Created April 17, 2014 20:31
an-exercise-in-refactoring-large-methods-in-ruby-2-example-8
require 'minitest/autorun'
describe UserPasswordHashCleaner do
subject { UserPasswordHashCleaner }
describe "call(params)" do
it "will return a hash without the password key if it it's value is blank" do
params = { user: { password: "", admin: true } }
subject.call(params).must_equal({ user: { admin: true } })
end
@thatrubylove
thatrubylove / example-9.rb
Created April 17, 2014 20:32
an-exercise-in-refactoring-large-methods-in-ruby-2-example-9
module UserPasswordHashCleaner
extend self
def call(params)
...
strip_password_keys_from_hash(params)
end
protected
@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