Skip to content

Instantly share code, notes, and snippets.

View phiggins's full-sized avatar

pete higgins phiggins

View GitHub Profile
@phiggins
phiggins / benchmark output
Last active December 15, 2015 20:19
Patch to allow Ruby's Array#map to take a symbol.
$ ./ruby -I. -Ilib/ -w map_symbol_bench.rb
Rehearsal --------------------------------------------
nil 0.050000 0.000000 0.050000 ( 0.046891)
identity 0.880000 0.000000 0.880000 ( 0.887874)
block 1.080000 0.000000 1.080000 ( 1.078556)
to_proc 0.990000 0.000000 0.990000 ( 0.996956)
----------------------------------- total: 3.000000sec
user system total real
nil 0.040000 0.000000 0.040000 ( 0.045237)
@phiggins
phiggins / gist:5251001
Last active December 15, 2015 11:09
seattlerb omg
Hello ruby friends,
Next Tuesday night is Seattle.rb's monthly talkie meetup! Things will be going as planned with one big exception:
Ryan, Aja and I will all be travelling to Mountain West Ruby Conf that night. Don't worry though, two of my Substantial
coworkers have offered to host in my stead. Cassie Schmitz and Shawn Dempsey will be the ones who will let you in the door,
tell you the wifi password, direct you towards the restrooms and urge you to leave at the end. Everything else will be the
same, although I cannot promise you'll hear another of Aaron's thrilling tales of candy purchasing.
Let me know if you have questions or concerns!
[14:42:11 projects]$ ruby my_o.rb
70255884061920: hello
1.9.1 :001 > Video.where(video_id: 1234).first_or_initialize.video_id
Video Load (2.0ms) SELECT `videos`.* FROM `videos` WHERE `videos`.`video_id` = '1234' LIMIT 1
=> "'1234'"
require 'rubygems'
require 'benchmark/ips'
class ExampleClass
def foo; 42; end
end
non_dci_object = ExampleClass.new
module ExampleMixin
@phiggins
phiggins / output
Created December 2, 2012 19:22
Bad error message using rspec's #=~ on hashes
pete@balloon:~/projects/rspec-expectations$ rspec rspec_test.rb
FF
Failures:
1) Hash #=~ displays confusing error message when hashes are equal
Failure/Error: actual.should =~ actual
expected: {:foo=>"bar"}
got: {:foo=>"bar"} (using =~)
Diff:{:foo=>"bar"}.==({:foo=>"bar"}) returned false even though the diff between {:foo=>"bar"} and {:foo=>"bar"} is empty. Check the implementation of {:foo=>"bar"}.==.
@phiggins
phiggins / sql_test.rb
Last active October 12, 2015 14:57
Measure ActiveRecord queries executed in a block
Organization.where("name != 'national'")
email = "foo@bar.com"
User.where(email: email).destroy_all
def sql_queries(title, verbose=false)
queries = Hash.new(0)
callback = lambda {|*args|
sql = args.last[:sql]
@phiggins
phiggins / gist:3687249
Created September 9, 2012 20:55
Rails tricks
One that seems to surprise a lot of people is Hash.from_xml:
> hash = {:foo => 'bar', :baz => 'qux'}
=> {:foo=>"bar", :baz=>"qux"}
> hash.to_xml
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n
<foo>bar</foo>\n <baz>qux</baz>\n</hash>\n"
> Hash.from_xml(hash.to_xml)
=> {"hash"=>{"foo"=>"bar", "baz"=>"qux"}}
@phiggins
phiggins / config_example_open_struct.rb
Created August 29, 2012 06:44
Class configuration using OpenStruct
require 'ostruct'
module Awesome
def self.config
@config
end
def self.configure
yield(config)
end
@phiggins
phiggins / config_example_method_missing.rb
Created August 29, 2012 06:41
Class configuration using method_missing
module Awesome
@config = {}
def self.configure
yield(self)
end
def self.method_missing method, *args, &block
if method.to_s[-1] == '='
@config[method[0..-2].to_sym] = args.first