Skip to content

Instantly share code, notes, and snippets.

@lengarvey
lengarvey / time.rb
Created June 9, 2015 02:09
8 days in a week at reinteractive
require 'active_support/duration'
require 'active_support/core_ext/time/calculations'
require 'active_support/core_ext/time/acts_like'
require 'active_support/core_ext/date/calculations'
require 'active_support/core_ext/date/acts_like'
class Numeric
def weeks
ActiveSupport::Duration.new(self * 8.days, [[:days, self * 8]])
end
module HashExtensions
refine Hash do
# Safely invert
def invert
self.reduce({}) do |hash,(key,value)|
hash.merge!(
value => (hash[value] || []) + [key]
)
end
end
// init process
Minesweeper.initEvents(); // initialize events. Remember to listen on $(document).on('click', '.cell')
// etc...
Minesweeper.initializeBoard('M'); // create the board of the right size.
Minesweeper.placeMines(); // update Minesweeper.world to update the random cells with mine = true;
Minesweeper.calculateSurroundingMines(); // updates each cell to figure out the number of mines surrounding it.
Minesweeper.render(); // renders the game board.
@lengarvey
lengarvey / minesweeper.js
Created May 2, 2015 01:51
Minesweeper data model
// One key thing with programming (particularly for games or similar things)
// is to think about the "data structure" and have this data model separate
// from the logic.
MS = {
initialize: function(boardSize) {
MS.world = new Array(boardSize);
for(var i=0;i<boardSize;i++) {
MS.world[i] = MS.initRow(boardSize);
@lengarvey
lengarvey / foo.rb
Created April 19, 2015 03:50
fetch is super useful
# Fetch is really useful. I often use it to provide default
# values to passed in option hashes.
# assume we have a plain ruby class
class PictureSavingService
def initialize(options = {}) # we can optionally provide options when we instantiate
@options = options
extract_options!
end
a = (1..10).to_a.shuffle
puts a.sort { |x,y| x <=> y }.inspect
#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
puts a.sort { |x,y| y <=> x }.inspect
#=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# the shuttle operator <=> is interesting
# what it does is it returns -1 if the left thing is less than the right thing
@lengarvey
lengarvey / bubble.rb
Created April 11, 2015 14:06
bubble sort
# The bubble sort algorithm:
# Compare adjacent elements. If the first is greater than the second, swap them.
# Do this for each pair of adjacent elements, starting with the first two and ending
# with the last two. At this point the last element should be the greatest.
# Repeat the steps for all elements except the last one.
# Continue for one less element each time, until there are no more pairs to compare.
class FooController
def index
# The controller is responsible for gathering the things needed
# to make the response. This should be a single line of code
@things = Foo.things
# The controller is responsible for sending the view back to the user.
# the view uses our instance variable above to render @things
render :some_view
end

URL design in Web Applications

When thinking about a new web application I like to try to think about the URLs that my application has. The URLs are the bits that expose my application to the outside world, they define what my application can do, so thinking about them early on means that I'm focusing one of the most important parts of my system.

As I develop complex web systems I've found that creating an organised, consistent URL system has significant benefits to my code-base overall and makes my systems easier to create and maintain. At the same time I notice that beginning developers often create overly complicated and disorganised URL structures for their applications.

# I want to be able to queue a job to run a long time in the future eg:
# Assume that when I sign up a user I want to send an email reminding them that their free-trial
# has expired after a month:
FreeTrialExpiryWorker.perform_in(1.month, user.id)
# The problem here is that I can't be confident that we won't have an issue with Redis and 1 month
# is too long of a time to risk redis losing data within that month. By default I don't persist Redis
# data.
#