Skip to content

Instantly share code, notes, and snippets.

@odlp
odlp / module-example.rb
Last active December 24, 2015 16:09
Ruby - examples of cascading & namespacing
# EXAMPLE 1: Cascade
# Two modules happen to have the same method ('say').
# If we include the Shouting module last, the Shouting 'say' method overwrites the Talkable 'say' method.
module Talkable
def say (str)
puts str
end
end
@odlp
odlp / tenline.rb
Created October 24, 2013 09:58
typing.io tenline
puts `clear`
exercises = ["How much wood can a woodchuck chuck?", "What's the time Mr.Wolf?", "Peter Piper picked a peck of pickled peppers."]
def check_diff text, input
text.length.times.map{|i| 1 unless text.chars[i] == input.chars[i]}.count(1) + (input.length > text.length ? input.length - text.length : 0)
end
exercises.each do |exercise|
puts "Type the following:\n---\n#{exercise}\n---\n\n"
start_time, input, end_time = Time.now, gets.chomp, Time.now
puts "Mistakes: #{check_diff(exercise, input)} characters. Time taken: #{(end_time - start_time).round(2)} seconds.\n\n"
end
@odlp
odlp / gist:6d6c951745e3c1e0226a
Last active August 29, 2015 14:03
Convert CSV file encoding to UTF-8 (incov)
# Convert file encoding for CSV in OS X:
iconv -f iso-8859-1 -t UTF-8 original.csv > utf-output.csv
# Where
# -f is current encoding
# -t is target encoding
# Guestimate of original file encoding with:
file -I
@odlp
odlp / gist:e02621612aae82a778c2
Created August 20, 2014 09:29
Distinct substring SQL query with Postgresql
result = ActiveRecord::Base.connection.execute("SELECT DISTINCT SUBSTRING(postcode, '[A-Z0-9]{3,} [0-9]{1}') FROM postcodes")
result.values.first
# => ['AB1 1']
@odlp
odlp / gist:6504ffbc25683fa8e1a3
Created August 21, 2014 09:04
Useful postgresql commands
# Export table to CSV from psql:
COPY table_name TO 'filepath' WITH DELIMITER AS ',' CSV HEADER;
@odlp
odlp / gist:19d750af7d185bb1bd74
Created August 28, 2014 14:22
ISO8601 string
def iso8601_string? datetime_str
(datetime_str =~ /\A\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)\z/) == 0
end
@odlp
odlp / x_controller_spec.rb
Last active October 13, 2015 16:47
RSpec Controller spec request type CONTENT_TYPE
before(:each) { @request.env["CONTENT_TYPE"] = "application/json" }
@odlp
odlp / options_request.rb
Created January 18, 2015 08:57
RSpec Options (CORS) request support
# Add support for 'options' request test. Place in /spec/support
# https://github.com/rspec/rspec-rails/issues/925#issuecomment-62527611
module ActionDispatch::Integration::RequestHelpers
def options(path, parameters = nil, headers_or_env = nil)
process :options, path, parameters, headers_or_env
end
end
@odlp
odlp / brewstrap.rb
Created November 5, 2015 16:01
Brewstrap: Taps the taps, installs the formulae & casks
#!/usr/bin/env ruby
require 'yaml'
CONFIG_FILE_NAME = 'brewstrap.yml'
COMMAND_INSTALL_CASK = 'brew install caskroom/cask/brew-cask'
COMMAND_BREW_UPDATE = 'brew update'
COMMAND_FORMULA_INSTALL = 'brew install'
COMMAND_BREW_TAP = 'brew tap'
COMMAND_BREW_CASK_INSTALL = 'brew cask install --appdir=/Applications'
@odlp
odlp / ci.sh
Last active April 29, 2016 10:53
iOS build on CI
#!/bin/sh
set -e
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# Ensure Jenkins is using rbenv
export PATH=/usr/local/bin:$PATH
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi