Skip to content

Instantly share code, notes, and snippets.

View joshsaintjacque's full-sized avatar
🏠
Working from home

Josh Saint Jacque joshsaintjacque

🏠
Working from home
View GitHub Profile
@joshsaintjacque
joshsaintjacque / do_one_thing_before.rb
Created August 19, 2017 16:53
A hypothetical example of a method that's too long
class User < TotallyNotActiveRecord
# ...Other functions on the User model...
def create(data=nil)
# Create the user record in the database
super(data)
# Send a welcome email
WelcomeEmail.send(email) if email.present?
@joshsaintjacque
joshsaintjacque / strategy_pattern_after.rb
Created June 26, 2017 19:38
Strategy Pattern: After
class Driver
attr_reader :vehicle
def initialize(vehicle)
@vehicle = vehicle
end
def floor_it
vehicle.accelerate_to(120)
end
@joshsaintjacque
joshsaintjacque / strategy_pattern_before.rb
Created June 26, 2017 19:29
Strategy Pattern Example: Before
class Driver
attr_reader :car
def initialize
@car ||= Car.new
end
def floor_it
car.accelerate_to(120)
end
@joshsaintjacque
joshsaintjacque / text_report_and_html_report_example.rb
Created June 11, 2017 06:09
TextReport and HTMLReport example
class TextReport < ReportTemplate
def format_report
# arrange financial data into plain text report
end
end
class HTMLReport < ReportTemplate
def format_report
# arrange financial data into HTML report
end
@joshsaintjacque
joshsaintjacque / report_template_example.rb
Created June 11, 2017 06:08
Report template example
class ReportTemplate
def generate_report!
retrieve_financial_data
format_report
send_to_stakeholders
end
private
def retrieve_financial_data
@joshsaintjacque
joshsaintjacque / report_example.rb
Last active June 11, 2017 06:00
Report example
class Report
def generate_report!
retrieve_financial_data
format_report
send_to_stakeholders
end
private
def retrieve_financial_data
@joshsaintjacque
joshsaintjacque / dock_spacer
Created February 28, 2017 23:45
Add spacer to dock
defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="spacer-tile";}' && killall Dock
@joshsaintjacque
joshsaintjacque / array_iteration_thoughts.md
Created January 12, 2017 21:53 — forked from ljharb/array_iteration_thoughts.md
Array iteration methods summarized

While attempting to explain JavaScript's reduce method on arrays, conceptually, I came up with the following - hopefully it's helpful; happy to tweak it if anyone has suggestions.

Intro

JavaScript Arrays have lots of built in methods on their prototype. Some of them mutate - ie, they change the underlying array in-place. Luckily, most of them do not - they instead return an entirely distinct array. Since arrays are conceptually a contiguous list of items, it helps code clarity and maintainability a lot to be able to operate on them in a "functional" way. (I'll also insist on referring to an array as a "list" - although in some languages, List is a native data type, in JS and this post, I'm referring to the concept. Everywhere I use the word "list" you can assume I'm talking about a JS Array) This means, to perform a single operation on the list as a whole ("atomically"), and to return a new list - thus making it much simpler to think about both the old list and the new one, what they contain, and

@joshsaintjacque
joshsaintjacque / partition.rb
Last active December 23, 2015 18:37
Given an array of integers and a partition index, switch the two partitions of the array in O(1) space and O(n) time.
# Solution to https://www.reddit.com/r/csinterviewproblems/comments/3xfzwt/given_an_array_of_integers_and_a_partition_index/
# Given an array of integers and a partition index, switch the two partitions of the array in O(1) space and O(n) time.
def partition(enum, enum_index)
enum.partition.with_index {|value, index| index >= enum_index}.flatten
end
@joshsaintjacque
joshsaintjacque / javascript_resources.md
Created May 8, 2014 19:36 — forked from jookyboi/javascript_resources.md
Here are a set of libraries, plugins and guides which may be useful to your Javascript coding.

Libraries

  • jQuery - The de-facto library for the modern age. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.
  • Backbone - Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
  • AngularJS - Conventions based MVC framework for HTML5 apps.
  • Underscore - Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects.
  • lawnchair - Key/value store adapter for indexdb, localStorage