Skip to content

Instantly share code, notes, and snippets.

View phiggins's full-sized avatar

pete higgins phiggins

View GitHub Profile
[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
@phiggins
phiggins / broken_sexp_sort.rb
Created August 1, 2012 06:17
broken_sexp_sort.rb
require 'rubygems'
require 'sexp_processor'
#def uses_if
# if some_func
# if some_other_func
# return
# end
# end
#end
@phiggins
phiggins / gist:2958393
Created June 20, 2012 06:10
heckle ruby_parser failures
pete@balloon ~/projects/heckle $ rake
/home/pete/.rvm/rubies/ruby-1.8.7-p358/bin/ruby -w -I../../ParseTree/dev/lib:../../ParseTree/dev/test:../../RubyInline/dev/lib:../../ruby2ruby/1.3.1/lib:../../ZenTest/dev/lib:../../sexp_processor/dev/lib:lib:lib:bin:test:. -e 'require "rubygems"; require "test/unit"; require "test/test_heckle.rb"' --
Loaded suite -e
Started
...............EE........F..F..............
Finished in 14.142885 seconds.
1) Error:
test_default_structure(TestHeckleClassMethod):
NoMethodError: undefined method `first' for nil:NilClass