Skip to content

Instantly share code, notes, and snippets.

View dwbutler's full-sized avatar

David Butler dwbutler

View GitHub Profile
@dwbutler
dwbutler / gist:9774171
Created March 26, 2014 00:03
Benchmark a constant vs a method
RUBY_VERSION
# => "2.1.1"
require 'benchmark'
DEFAULT_OPTIONS = {validate: true}
def default_options
{validate: true}
end
@dwbutler
dwbutler / iso8601.rb
Last active August 29, 2015 14:01
Parsing ISO8601 Time format with Ruby
require 'time'
require 'benchmark'
time = Time.now.iso8601(3)
# => "2014-05-26T23:26:08.628-07:00"
Benchmark.bm do |x|
x.report('parse') { 100_000.times { Time.parse(time).gmtime } }
x.report('iso8601') { 100_000.times { Time.iso8601(time).gmtime } }
end
# Tested with ActiveRecord 4.0.5
# Run using:
# BUNDLE_GEMFILE=gemfiles/rails_4.0.gemfile bundle console
require 'sqlite3'
require 'active_record'
require 'groupify/adapter/active_record'
ActiveSupport::VERSION::STRING
# => "4.0.5"
@dwbutler
dwbutler / 0. Platform
Last active December 10, 2015 21:48
Trying to get FFI working with the latest version of Rubinius
uname -a
Darwin dbutler-mac-2.local 12.2.0 Darwin Kernel Version 12.2.0: Sat Aug 25 00:48:52 PDT 2012; root:xnu-2050.18.24~1/RELEASE_X86_64 x86_64
@dwbutler
dwbutler / gist:4499941
Last active December 10, 2015 22:08
Trying to get RDF::Raptor to run under FFI 1.3.0. This gist documents https://github.com/ffi/ffi/issues/245

##Issue ffi/ffi#245

TypeError: no implicit conversion from nil to integer

##Platform

uname -a
Darwin dbutler.local 12.2.0 Darwin Kernel Version 12.2.0: Sat Aug 25 00:48:52 PDT 2012; root:xnu-2050.18.24~1/RELEASE_X86_64 x86_64
@dwbutler
dwbutler / JSON_gem.rb
Last active December 11, 2015 17:08
Trying to reproduce a JSON.dump bug in JRuby
rvm use jruby
gem install json
Successfully installed json-1.7.6-java
1 gem installed
# Uses JSON prepackaged with JRuby?
➜ ~ jruby -r 'json' -r 'active_support/hash_with_indifferent_access' -e "puts JSON::VERSION; puts JSON.dump HashWithIndifferentAccess.new"
1.7.5
{}
@dwbutler
dwbutler / appcanary.rb
Last active December 14, 2015 23:17
Appcanary gem interface
require 'appcanary'
Appcanary.configure do |config|
config.api_key = ENV['APPCANARY_API_KEY']
end
# Explicitly specify language and file
Appcanary.check(language: :ruby, file: 'Gemfile.lock')
# Autodetect Ruby as the language and Gemfile.lock as the file
@dwbutler
dwbutler / Cost of defined? in JRuby.rb
Last active December 29, 2015 19:18
Cost of defined? in JRuby
require 'benchmark'
Benchmark.bm {|x| x.report { 1_000.times { defined?(::UNF::Normalizer) } } }
user system total real
0.300000 0.010000 0.310000 ( 0.340000)
@dwbutler
dwbutler / db.rake
Created August 28, 2013 22:56 — forked from ches/db.rake
namespace :db do
desc 'Open a MongoDB console with connection parameters for the current Rails.env'
task :console, [:session] => :environment do |t, args|
session_name = args[:session] || :default
config = Mongoid.sessions[session_name]
username = config[:username]
password = config[:password]
database = config[:database]
@dwbutler
dwbutler / overwritten.rb
Last active January 4, 2017 04:52
Find where a variable is overwritten with a different value
class Config
attr_accessor :thing
def thing=(val)
raise ArgumentError if defined?(@thing)
@thing = val
end
end
config = Config.new