Skip to content

Instantly share code, notes, and snippets.

View cjbottaro's full-sized avatar

Christopher J. Bottaro cjbottaro

  • AcademicWorks, Inc
  • Austin, Tx
View GitHub Profile
@cjbottaro
cjbottaro / gist:1167520
Created August 24, 2011 07:56
sleep in EM reactor
EM.run do
t = Time.now
Fiber.new{ sleep(1) }.resume
Fiber.new{ sleep(1) }.resume
puts Time.now - t
# 2 seconds have elapsed. No concurrency is achieved.
EM.stop
end
@cjbottaro
cjbottaro / gist:1205075
Created September 8, 2011 23:20
class vs instance vars in CoffeeScript
class Blah
@counter = 0
@incr: ->
return @counter += 1
constructor:
@counter = 0
@cjbottaro
cjbottaro / gist:1326224
Created October 30, 2011 18:21
Wish this worked
module User::Ascents
extend ActiveSupport::Concern
included do
has_many :fun_ascents, :class_name => "Ascent"
end
def self.extended(object)
object.singleton_class.send(:include, self)
end
@cjbottaro
cjbottaro / gist:1326226
Created October 30, 2011 18:22
No dice either
module User::Ascents
def self.extended(object)
object.singleton_class.class_eval do
has_many :fun_ascents, :class_name => "Ascent"
end
end
end
@cjbottaro
cjbottaro / gist:1361613
Created November 13, 2011 04:51
god segfault
$ sudo RAILS_ENV=staging bundle exec god -c config/god/resque.rb -D
I [2011-11-13 04:46:05] INFO: Loading config/god/resque.rb
I [2011-11-13 04:46:05] INFO: Syslog enabled.
I [2011-11-13 04:46:05] INFO: Using pid file directory: /home/sender/sender_staging/current/tmp/pids
I [2011-11-13 04:46:05] INFO: Started on drbunix:///tmp/god.17165.sock
I [2011-11-13 04:46:05] INFO: resque-00 move 'unmonitored' to 'init'
I [2011-11-13 04:46:05] INFO: resque-00 moved 'unmonitored' to 'init'
I [2011-11-13 04:46:05] INFO: resque-00 [trigger] process is running (ProcessRunning)
I [2011-11-13 04:46:05] INFO: resque-00 move 'init' to 'up'
I [2011-11-13 04:46:05] INFO: resque-00 registered 'proc_exit' event for pid 8972
@cjbottaro
cjbottaro / gist:1488185
Created December 16, 2011 21:56
convert to utf-8 using iconv fallback when String#encode doesn't work
require "iconv"
def sanitize_utf8(string)
string = string.encode("UTF-8", :invalid => :replace, :undef => :replace)
begin
string.blank? # Assume you're using ActiveSupport
rescue ArgumentError => e
if e.message == "invalid byte sequence in UTF-8"
Thread.current["iconv"] ||= Iconv.new('UTF-8//IGNORE', 'UTF-8')
string = Thread.current["iconv"].iconv(string)
@cjbottaro
cjbottaro / gist:1720115
Created February 1, 2012 23:22
DCI with ActiveSupport callbacks
require "active_support"
module RoleA
def self.extended(object)
object.singleton_class.class_eval do
puts "blah"
puts object.inspect
include ActiveSupport::Callbacks
define_callbacks :save
@cjbottaro
cjbottaro / gist:1760846
Created February 7, 2012 17:29
bug with Queue
# ruby-1.9.3-p0
# Run in IRB...
require "thread"
q = Queue.new
q.pop # fatal: deadlock detected
q.pop # blocks properly
<table class="normalize-size">
<<<<<<< HEAD
<tbody>
<tr>
<td><%= t("oa.users.system.display_name") %></td>
<td><%= user.display_name %></td>
</tr>
<tr>
@cjbottaro
cjbottaro / gist:2926951
Created June 13, 2012 22:41
Adding methods to ActiveRecord::Relation to wrap results
require "spec_helper"
class RecordWrapper
def initialize(record)
@record = record
end
end
module RelationWrapping