Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrew White pixeltrix

View GitHub Profile
@pixeltrix
pixeltrix / time_vs_datatime.md
Last active Aug 4, 2022
When should you use DateTime and when should you use Time?
View time_vs_datatime.md

When should you use DateTime and when should you use Time?

It's a common misconception that [William Shakespeare][1] and [Miguel de Cervantes][2] died on the same day in history - so much so that UNESCO named April 23 as [World Book Day because of this fact][3]. However because England hadn't yet adopted [Gregorian Calendar Reform][4] (and wouldn't until [1752][5]) their deaths are actually 10 days apart. Since Ruby's Time class implements a [proleptic Gregorian calendar][6] and has no concept of calendar reform then there's no way to express this. This is where DateTime steps in:

>> shakespeare = DateTime.iso8601('1616-04-23', Date::ENGLAND)
=> Tue, 23 Apr 1616 00:00:00 +0000
>> cervantes = DateTime.iso8601('1616-04-23', Date::ITALY)
=> Sat, 23 Apr 1616 00:00:00 +0000
@pixeltrix
pixeltrix / truffleruby-logo.svg
Last active Jul 27, 2021
Truffle logo in SVG with outline shadow layer
View truffleruby-logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pixeltrix
pixeltrix / routes.rb
Created Oct 29, 2010
Examples of advanced Rails 3.0 routes
View routes.rb
Rails.application.routes.draw do
get '/(:locale)/products/(:category)/(page/:page).:extension',
:to => 'products#index',
:as => :products,
:constraints => {
:locale => /[a-z]{2}/,
:category => /.+?/,
:page => /\d+/
},
@pixeltrix
pixeltrix / activations_controller.rb
Last active May 26, 2020
Example of a 'service' object
View activations_controller.rb
class ActivationsController < ApplicationController
respond_to :json
def create
new_password = params[:user] && params[:user][:password]
token = params[:confirmation_token]
if !new_password
render_errors({"password"=>["can't be blank"]}.to_json)
@pixeltrix
pixeltrix / anpr.rb
Created May 13, 2020
Match one of three number plate patterns
View anpr.rb
formats = [
"[A-Z]{2}[0-9]{2}[A-Z]{3}",
"[A-Z]{1}[0-9]{3}[A-Z]{3}",
"[A-Z]{3}[0-9]{3}[A-Z]{1}"
]
pattern = /\A#{formats.join("|")}\z/
candidates = %w[
RF10AK0
@pixeltrix
pixeltrix / welsh-constituencies.topojson
Created May 11, 2020
Welsh Electoral Constituencies (TopoJSON Format)
View welsh-constituencies.topojson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pixeltrix
pixeltrix / welsh-regions.topojson
Created May 11, 2020
Welsh Electoral Regions (TopoJSON Format)
View welsh-regions.topojson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pixeltrix
pixeltrix / welsh-constituencies.geojson
Created May 11, 2020
Welsh Electoral Constituencies
View welsh-constituencies.geojson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pixeltrix
pixeltrix / welsh-regions.geojson
Last active May 11, 2020
Welsh Electoral Regions
View welsh-regions.geojson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pixeltrix
pixeltrix / sonar.rb
Last active May 9, 2020
Ruby implementation of the NHS contact tracing app's messages and how they're decrypted
View sonar.rb
require "openssl"
require "securerandom"
##########################
### iOS/Android Device ###
##########################
# Installation id - returned by the registration request
uuid = "E1D160C7-F6E8-48BC-8687-63C696D910CB"
uuid_bytes = uuid.scan(/[0-9A-Z]{2}/).map { |s| s.to_i(16) }.pack("C*")