Skip to content

Instantly share code, notes, and snippets.

View nickhoffman's full-sized avatar

Nick Hoffman nickhoffman

  • Toronto, Canada
View GitHub Profile
@nickhoffman
nickhoffman / run.rb
Last active April 15, 2016 19:09
Code Kata: string splitter
class String
def multiline_split(max_line_length)
self
end
end
puts "foo\nbar\nbaz" == "foo bar baz".multiline_split(3)
puts "foo bar\nbaz" == "foo bar baz".multiline_split(9)
@nickhoffman
nickhoffman / Configure IRB's prompt for a Rails app
Last active December 15, 2015 09:49
Put each of these in ~/.irbrc
if defined? Rails
# Customize the IRB prompt.
short_env = case Rails.env
when 'development'
'dev'
when 'production'
'prod'
else
Rails.env
@nickhoffman
nickhoffman / gist:3015881
Created June 29, 2012 05:01
ElasticSearch: Parent/Child: Find children based on parent properties
#!/bin/bash
# Reset the index.
curl -X DELETE "localhost:9200/test/?pretty=true"
curl -X PUT "localhost:9200/test/?pretty=true"
# Create the parent mapping.
echo
curl -X PUT "localhost:9200/test/user/_mapping?pretty=true" -d '{
"user": {
module TrueFalseComparison
# Enable TrueClass and FalseClass values to be comparable.
# TrueClass values are valued higher than FalseClass values.
#
# See this article for more info:
# http://grosser.it/2010/07/30/ruby-true-false-comparison-with/
#
# Yes, this is evil monkey-patching. The alternative is much, much worse:
#
# [false, true, false].sort {|a, b| a ? (b ? 0 : -1) : (b ? 1 : 0) }
The first context
Creating Person with name Nick
✗ Errored » callback not fired
in Create a Person via JavaScript: When a person has a name,
in Creating a Person
in undefined✗ Errored » 1 errored ∙ 1 dropped
@nickhoffman
nickhoffman / gist:2481672
Created April 24, 2012 17:21
MySQL's "REPEATABLE READ" and "SELECT ... FOR UPDATE"
mysql> show create table numbers\G
*************************** 1. row ***************************
Table: numbers
Create Table: CREATE TABLE `numbers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`num` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
@nickhoffman
nickhoffman / gist:1666614
Created January 24, 2012 00:01
Nesting text within a content tag.
# The HTML that should be generated is:
# <h3 class="curated_by">Curated by <span>Bob</span></h3>
# Please tell me that there's a better way to do this.
def curated_by
html = ''
html += h.content_tag(:h3, :class => 'curated_by') do
h.concat h.t('phrases.Curated_by')
h.concat ' '
curl 'localhost:9200/development_products/product/_search?pretty=true' -d '
{
"query" : {
"dis_max" : {
"queries" : [
{ "field" : {"name" : "Arise!"}},
{ "field" : {"catalog.name" : "Arise!"}}
]
}
}
@nickhoffman
nickhoffman / option_1.rb
Created January 13, 2012 21:06
Would you duplicate the validation's regex, or reference a constant?
class User
field :username, :type => String
validates :username, :format => { :with => /\A[[:print:] ]{3,20}\Z/ }
end
class ScoreCard
belongs_to :user
field :username, :type => String
@nickhoffman
nickhoffman / gist:1590041
Created January 10, 2012 17:07
An alternative to halting in a helper
get '/users/:user_id/projects/:project_id/tasks-due-on/:task_date' do
user = User.find(params[:user_id])
halt 404 unless user
project = user.projects.find(params[:project_id])
halt 404 unless project
# etc...
end