Skip to content

Instantly share code, notes, and snippets.

View jeremy's full-sized avatar

Jeremy Daer jeremy

View GitHub Profile
From 273f56bd6c23bea5ca910af689aa44d4ca56cb7b Mon Sep 17 00:00:00 2001
From: Jeremy Kemper <jeremy@bitsweat.net>
Date: Fri, 27 Feb 2009 14:13:32 -0800
Subject: [PATCH 1/4] Rather than check socket.closed? on every read/write, connect once and reconnect on exceptions
---
lib/redis.rb | 45 ++++++++++++++++++++++++---------------------
1 files changed, 24 insertions(+), 21 deletions(-)
diff --git a/lib/redis.rb b/lib/redis.rb
@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 / 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:
require 'active_support/concern'
module Touchless
module TouchLater
extend ActiveSupport::Concern
included do
# Preserve immediate touch behavior as touch_now
alias_method :touch, :touch_now
alias_method :touch_later, :touch
@jeremy
jeremy / ruby-2.5.txt
Last active November 25, 2019 17:16
bcrypt-ruby bc_salt segfault
-- C level backtrace information -------------------------------------------
/opt/ruby2.5.1/bin/../lib/libruby.so.2.5(rb_vm_bugreport+0x974) [0x7f6e7aad0904]
/opt/ruby2.5.1/bin/../lib/libruby.so.2.5(0xaaec4) [0x7f6e7a941ec4]
/opt/ruby2.5.1/bin/../lib/libruby.so.2.5(0x1a89c2) [0x7f6e7aa3f9c2]
/lib/x86_64-linux-gnu/libc.so.6(0x7f6e7a4e4f20) [0x7f6e7a4e4f20]
/lib/x86_64-linux-gnu/libc.so.6(__strlen_avx2+0x11) [0x7f6e7a6345a1] ../sysdeps/x86_64/multiarch/strlen-avx2.S:59
/opt/ruby2.5.1/bin/../lib/libruby.so.2.5(rb_str_new_cstr+0xe) [0x7f6e7aa5712e]
/u/apps/bc3/shared/bundle-ruby221/ruby/2.5.0/gems/bcrypt-3.1.13/lib/bcrypt_ext.so(bc_salt+0x6a) [0x7f6e6ad2b04a]
@jeremy
jeremy / gist:4211803
Created December 5, 2012 03:05
Template- and asset-aware ETags

Declared ETags, together with Russian Doll caching, can be used to automatically mix your template and asset versions into the ETags set in your controllers. This avoids the need to blow all browser caches on each deploy and neatly contains the scope of "freshness fallout" when you tweak a view.

To include the template's version in the ETag:

  # Incorporate the cache version for this action into our ETag.
  # This allows template changes to bubble up into HTTP cache
  # freshness and bust browser caches when we make changes.
  etag do
    begin
@jeremy
jeremy / reraised_backtrace.rb
Created August 20, 2016 02:14
Reraising an exception never amends its backtrace
def foo
e = RuntimeError.new
def e.backtrace() %w[ lol rofl ] end
raise e
rescue => e
raise e
end
foo
@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 -->

Keybase proof

I hereby claim:

  • I am jeremy on github.
  • I am bitsweat (https://keybase.io/bitsweat) on keybase.
  • I have a public key ASAHmrs7GVnFuLVA6XK5t2znEMLHrRiixbMEQsbT2wB0dwo

To claim this, I am signing this object:

@jeremy
jeremy / gist:4206626
Created December 4, 2012 17:34
irb rake
diff --git a/railties/lib/rails/console/helpers.rb b/railties/lib/rails/console/helpers.rb
index 230d3d9..1bbdfd2 100644
--- a/railties/lib/rails/console/helpers.rb
+++ b/railties/lib/rails/console/helpers.rb
@@ -7,5 +7,16 @@ module Rails
def controller
@controller ||= ApplicationController.new
end
+
+ def rake(task)