Skip to content

Instantly share code, notes, and snippets.

@the-undefined
the-undefined / fetch_and_delete_stale_branches
Created August 12, 2013 10:52
When fetching in git use `-p` flag to delete stale local branches (when their tracking branches have been deleted on the remote repo).
$ git fetch -p
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 1 (delta 0)
Unpacking objects: 100% (1/1), done.
f9b6959..3ff6575 master -> origin/master
x [deleted] (none) -> origin/admin-review-purchase-orders
x [deleted] (none) -> origin/creator-email-notifications
x [deleted] (none) -> origin/new-purchase-order
x [deleted] (none) -> origin/po-status-changes
@the-undefined
the-undefined / attr_capcessor.rb
Created August 20, 2013 20:54
Take advantage of metaprogramming and avoid littering the logic of your code with `define_method` declarations.
module Cappy
def attr_capcessor attr
attr_reader attr.to_sym
define_method "#{attr}=" do |value|
instance_variable_set("@#{attr}", value.to_s.upcase)
end
end
end
@the-undefined
the-undefined / n_plus_one_inserts.rb
Created September 2, 2013 20:22
Multiple record attribute hashes handed to an ActiveRecord model create method uses multiple SQL Insert statements, disappointingly.
Item.create(name: 'one record, one query') # One SQL Insert Query
multiple_items = [{ name: 'record one'}, { name: 'record two' }]
Item.create(multiple_items) # Two Consecutive SQL Insert Queries
@the-undefined
the-undefined / mutliple_nested_dir_creation.sh
Created September 2, 2013 21:23
Create multiple dirs within a parent dir
mkdir site/public/{images,js,css}
ls site/public
images js css
@the-undefined
the-undefined / minitest_vs_rspec_example
Created October 18, 2013 10:24
The main difference between Minitest and Rspec for ME is the conciseness of the assertion you can impose using Rspec.
# Minitest
it "should create a new credit card" do
from_hash = CreditCard.create_from_hash(hash)
assert from_hash.valid?
assert from_hash.number == number
assert from_hash.cvv == cvv
assert from_hash.expiry_month == expiry_month
assert from_hash.expiry_year == expiry_year
end
@the-undefined
the-undefined / shoes_app_crash_report
Created November 12, 2013 17:29
opening a packaged shoes 4 app crash report.
Process: JavaAppLauncher [69398]
Path: /Users/USER/*/app.app/Contents/MacOS/JavaAppLauncher
Identifier: com.hackety.shoes.app
Version: 1.0 (0.0.0)
Code Type: X86-64 (Native)
Parent Process: launchd [233]
Responsible: JavaAppLauncher [69398]
User ID: 501
Date/Time: 2013-11-12 17:27:24.529 +0000
@the-undefined
the-undefined / invite.rb
Last active January 1, 2016 00:39
Creating a DSL inspired by the rspec assertions but with a conversion method thrown in for added semantic measure.
module Invite
module_function
def Invite(people)
Invitees.new(people)
end
class Invitees
attr_reader :recipients
def initialize people
@the-undefined
the-undefined / last_tracks.rb
Created February 20, 2014 11:56
Thinking on a further refactoring on a nice blog post regarding flog, using a Session Model came to mind to assist with the responsibilities. POST: http://cored.github.io/blog/2014/02/19/my-gpa-at-code-climate-is-3-dot-59-a-refactoring-story
class LastTracks
attr_accessor :session, :list
def initialize(session)
@session = session
@list = []
yield(self)
end
def blacklist(url)
@the-undefined
the-undefined / time_travel_by_delorean.rb
Last active August 29, 2015 13:57
Time travelling in style - got any garbage?
class Delorean
# set_coodinates
# ==============
#
# ## REQUIRES:
#
# - abbr_time => "07:30"
#
# ## RETURNS:
# An instance of Delorean.
@the-undefined
the-undefined / repository_pattern.rb
Last active August 29, 2015 14:02
Implement the repository pattern with a strategy object (to swap out with an AR model)
class InMemoryStore
attr_accessor :result_set
def initialize
@result_set = []
end
def store(row)
!!(result_set << row)
end