Skip to content

Instantly share code, notes, and snippets.

View database_log_subscriber.rb
class DatabaseLogSubscriber < ActiveSupport::LogSubscriber
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
def sql(event)
return unless logger.debug?
payload = event.payload
return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
conn_id = payload[:connection_id]
conn = ObjectSpace._id2ref(conn_id)
from_connection = conn.active? ? conn.current_database : "No active connection"
View tournament_seeding.rb
teams = (1..16).to_a
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
games = (0..7).inject([]){|g, i| g << [teams[i], teams[-i-1]] }
# => [[1, 16], [2, 15], [3, 14], [4, 13], [5, 12], [6, 11], [7, 10], [8, 9]]
# so now, how do I transform that to:
# => [[1, 16], [8, 9], [5, 12], [4, 13], [6, 11], [3, 14], [7, 10], [2, 15]]
# In essence, I have the teams already sorted into the games of who plays who in this round.
# But I want to order the games so they appear next to who will play the winners of this round in the next round.
@codeodor
codeodor / reproduce_rails_issue_17119.rb
Last active Aug 29, 2015
A bug report script to reproduce issue #17119 in rails/rails
View reproduce_rails_issue_17119.rb
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'i18n', github: 'svenfuchs/i18n'
gem 'sqlite3'
GEMFILE
@codeodor
codeodor / bug_report.rb
Created Oct 2, 2014
Demo of bug described by rails/rails #17147
View bug_report.rb
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'i18n', github: 'svenfuchs/i18n'
gem 'sqlite3'
GEMFILE
@codeodor
codeodor / extract_archive.rb
Last active Jan 3, 2016
Proof of concept for adding extract_archive.rb to Fog for Rackspace
View extract_archive.rb
require 'fog'
module Fog
module Storage
class Rackspace
class Real
# Extract Archive
#
# See http://docs.rackspace.com/files/api/v1/cf-devguide/content/Extract_Archive-d1e2338.html
#
@codeodor
codeodor / houston_rb_december_2013.md
Last active Dec 30, 2015
Code for discussion @houstonrb December 2013 meetup.
View houston_rb_december_2013.md

I won't make it tonight because while I thought I'd be recovered from illness by now, I'm still a little sick and don't want to infect the entire Houston Ruby community with a nasty virus!

But I had some ideas for code, so if you need some, here they are:

These commits to Rails might seem like bad code because they take something elegant and turn it into longer uglier code, but the performance gains are nice!

rails/rails#12185

and

@codeodor
codeodor / trace-tools.txt
Last active Dec 29, 2015
rvm --trace upgrade && rvm --trace tools
View trace-tools.txt
~$ rvm --trace tools strings 1.9.3-p484
+ 1385768598.505099713 /scripts/cli : __rvm_parse_args() 671 > [[ -n '' ]]
+ 1385768598.514017915 /scripts/cli : __rvm_parse_args() 673 > set -o errtrace
+ 1385768598.523055186 /scripts/cli : __rvm_parse_args() 674 > export 'PS4=+ $(__rvm_date "+%s.%N" 2>/dev/null) ${BASH_SOURCE##${rvm_path:-}} : ${FUNCNAME[0]:+${FUNCNAME[0]}()} ${LINENO} > '
+ 1385768598.531905984 /scripts/cli : __rvm_parse_args() 674 > PS4='+ $(__rvm_date "+%s.%N" 2>/dev/null) ${BASH_SOURCE##${rvm_path:-}} : ${FUNCNAME[0]:+${FUNCNAME[0]}()} ${LINENO} > '
+ 1385768598.540891056 /scripts/cli : __rvm_parse_args() 708 > [[ -z '' ]]
+ 1385768598.549814287 /scripts/cli : __rvm_parse_args() 708 > [[ -n '' ]]
+ 1385768598.558420167 /scripts/cli : __rvm_parse_args() 711 > [[ error == '' ]]
+ 1385768598.567271382 /scripts/cli : __rvm_parse_args() 711 > [[ 0 -eq 1 ]]
+ 1385768598.576020986 /scripts/cli : __rvm_parse_args() 711 > [[ -n '' ]]
@codeodor
codeodor / gist:7101231
Created Oct 22, 2013
What I really want is $(‘option [value :in(1,2,3)]’) to select all options whose value is 1, 2, or 3.
View gist:7101231
Example:
select 2 has, say 10 options: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
select1_option0 should hide nothing in select2
select1_option1 should hide all but select2: options 1, 2, and 3
select1_option2 should hide all but select2: options 1, 2, 3, and 9
select1_option3 should hide all but select2: options 5, 6, and 7
select1_option4 should hide all but select2: options 4, 6, 8, and 1
@codeodor
codeodor / explanation.md
Last active Jan 6, 2016
I need help with an error between Net::FTP and OpenSSL in Ruby.
View explanation.md

I'm trying to get FTPFXP to work on Ruby 2.0 (so I can FTP over TLS)

I'm having problems with this line, which sets up the SSL Socket. OpenSSL checks whether @sock is a T_FILE

In Ruby 1.9.3, it worked well, because it was just a TCPSocket but in Ruby 2.0, @sock becomes a Net::FTP::BufferedSocket.

It's easy enough to change the code in https://github.com/codeodor/ftpfxp/blob/06f2d56e65e73d3818b6c7aef4dfe461bad98849/lib/ftpfxp/ftpfxptls.rb#L87 to send @sock.io to make it a TCPSocket again, but when Net::FTP tries to close the socket, it expects the socket to have a read timeout, wh

View bob.rb
require 'ripper'
def Object.const_missing(name)
klass = const_set name, Class.new
klass.class_eval do
def method_missing name, *args
file, line_num, _ = caller[0].split(':')
file_contents = File.open(file).read