Skip to content

Instantly share code, notes, and snippets.

View jeremy's full-sized avatar

Jeremy Daer jeremy

View GitHub Profile
# Configure generators to use sequel, haml, and shoulda.
config.generators do |g|
g.orm :sequel
g.template_engine :haml
g.test_framework :shoulda, :fixtures => true
end
@jeremy
jeremy / NewRelic custom dashboard
Created August 7, 2009 05:35
Track object allocations in NewRelic
<table width="100%">
<tr><td width="50%">
{% compare_with_last_week_chart metric:'Custom/Objects/Allocated' title:'Object Allocations' value:total_value %}
</td><td width="50%">
{% compare_with_last_week_chart metric:'Custom/Objects/Live' title:'Live Objects' value:average_value %}
</td></tr>
</table>
# Dependencies: ruby, git, rubygems
sudo gem install bundler
git clone git://github.com/rails/rails.git
cd rails && gem bundle
ruby -rvendor/gems/environment railties/bin/rails ../new_app
cd ../new_app
# edit Gemfile to use:
@jeremy
jeremy / schema.xml
Created April 2, 2011 00:53
Basecamp Solr schema
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="basecamp" version="1.3">
<types>
<!-- indexed/stored verbatim -->
<fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true" omitTermFreqAndPositions="true"/>
<!-- "true" or "false" -->
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" omitNorms="true" omitTermFreqAndPositions="true"/>
<!-- binary data, base64 -->
@jeremy
jeremy / required_routing_on_unmapped_field_issue.sh
Created September 23, 2011 04:02
elasticsearch required _routing must have an explicit field mapping for non-string fields
#!/usr/bin/env bash
# https://github.com/elasticsearch/elasticsearch/issues/1357
set -o verbose
curl -s -XGET http://localhost:9200/ |grep -B 1 number
curl -XDELETE http://localhost:9200/foo
curl -XPUT http://localhost:9200/foo -d '{
"mappings": {
@jeremy
jeremy / gist:1371416
Created November 16, 2011 21:11
ElasticSearch caching response?
$ curl http://10.11.0.6:9200/migrations/_settings
{"migrations":{"settings":{"index.number_of_shards":"1","index.auto_expand_replicas":"1-all","index.number_of_replicas":"2"}}}
$ curl http://10.11.0.6:9200/migrations/_settings?pretty=true
{
"migrations" : {
"settings" : {
"index.number_of_shards" : "1",
"index.auto_expand_replicas" : "0-all",
"index.number_of_replicas" : "0"
@jeremy
jeremy / gist:1383337
Created November 21, 2011 17:39
Using Rails log for RestClient.log
require 'restclient'
# RestClient logs using << which isn't supported by the Rails logger,
# so wrap it up with a little proxy object.
RestClient.log =
Object.new.tap do |proxy|
def proxy.<<(message)
Rails.logger.info message
end
end
@jeremy
jeremy / UAX29URLEmailTokenizerImpl31.getNextToken
Created June 13, 2012 19:26
elasticsearch index thread hung on UAX29URLEmailTokenizer
3572 zzForAction: {
3573 while (true) {
3574
3575 if (zzCurrentPosL < zzEndReadL)
3576 zzInput = zzBufferL[zzCurrentPosL++];
3577 else if (zzAtEOF) {
3578 zzInput = YYEOF;
3579 break zzForAction;
3580 }
3581 else {
@jeremy
jeremy / gist:3724459
Created September 14, 2012 20:17
queue consumer exception handler and refactoring
commit 63a12f72df1a7507f7a1923bdaa56b56b876b562
Author: Jeremy Kemper <[email protected]>
Date: Fri Sep 14 17:00:46 2012 -0700
Pass an exception_handler to queue consumers. Don't run jobs in synchronous & test queues; delegate to a consumer.
diff --git a/activesupport/lib/active_support/queueing.rb b/activesupport/lib/active_support/queueing.rb
index f397e1c..c4ff8b8 100644
--- a/activesupport/lib/active_support/queueing.rb
+++ b/activesupport/lib/active_support/queueing.rb
@jeremy
jeremy / gist:4035286
Created November 7, 2012 23:15
Enumerable#associate

We've seen lots of Ruby feature requests about converting an Enumerable to a Hash: to_h, map_hash, map_to, each_with_hash, etc. Let's look at one common, simple case of this.

Building a key/value mapping from a collection of keys is awkward. We commonly see Hash[*collection.map { |element| [element, calculate(element)] }] or collection.each_with_object({}) { |element, hash| hash[element] = calculate(element) }. Both are verbose. They require boilerplate code that's not relevant to the programmer's intent: to associate an enumerable of keys with calculated values.

Ruby has the idea of an association already: a key and value paired together. It's used by Array#assoc to look up a value from a list of pairs and by Hash#assoc to return a key/value pair. Building up a mapping of key/value pairs is associating keys with values.

So! Consider Enumerable#associate which builds a mapping by associating keys with values:

# Associate filenames with URLs. Before: