Skip to content

Instantly share code, notes, and snippets.

View phiggins's full-sized avatar

pete higgins phiggins

View GitHub Profile
@phiggins
phiggins / benchmark_append.rb
Created October 22, 2014 03:20
What is the fastest way to add one thing to an array without mutating the array?
require 'benchmark/ips'
a = (0..10).to_a
Benchmark.ips do |x|
x.report("push") { a.dup.push(100) }
x.report("shovel") { a.dup << 100 }
x.report("concat") { a.dup.concat([100]) }
x.report("+") { a + [100] }
x.report("unshift") { a.dup.unshift(100) }
require 'objspace'
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection adapter: "sqlite3",
database: ":memory:"
ActiveRecord::Base.connection.instance_eval do
create_table(:articles) { |t| t.string :name }
end
@phiggins
phiggins / gist:97089e6eb5ac40298439
Created September 28, 2014 07:50
allocation_tracer segfault
test_error.rb:3: [BUG] Segmentation fault at 0x00000000000000
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0005 p:---- s:0012 e:000011 CFUNC :inspect
c:0004 p:---- s:0010 e:000009 CFUNC :inspect
c:0003 p:---- s:0008 e:000007 CFUNC :p
c:0002 p:0024 s:0004 E:001dc8 EVAL test_error.rb:3 [FINISH]
c:0001 p:0000 s:0002 E:001f18 TOP [FINISH]
> elements = {a1: 'a', b1: 'b', b2: 'b', b3: 'b', c1: 'c'}
=> {:a1=>"a", :b1=>"b", :b2=>"b", :b3=>"b", :c1=>"c"}
> elements.group_by {|(_, val)| val }.flat_map {|(_, vals)| vals.shuffle }
=> [[:a1, "a"], [:b2, "b"], [:b3, "b"], [:b1, "b"], [:c1, "c"]]
> elements.group_by {|(_, val)| val }.flat_map {|(_, vals)| vals.shuffle }
=> [[:a1, "a"], [:b1, "b"], [:b3, "b"], [:b2, "b"], [:c1, "c"]]
> elements.group_by {|(_, val)| val }.flat_map {|(_, vals)| vals.shuffle }
=> [[:a1, "a"], [:b1, "b"], [:b3, "b"], [:b2, "b"], [:c1, "c"]]
> elements.group_by {|(_, val)| val }.flat_map {|(_, vals)| vals.shuffle }
=> [[:a1, "a"], [:b2, "b"], [:b1, "b"], [:b3, "b"], [:c1, "c"]]
diff --git a/rspec-support/lib/rspec/support/hunk_generator.rb b/rspec-expectations/lib/rspec/expectations/differ.rb
index 5b0d68d..cef9a1f 100644
--- a/rspec-support/lib/rspec/support/hunk_generator.rb
+++ b/rspec-expectations/lib/rspec/expectations/differ.rb
@@ -1,10 +1,7 @@
-require 'diff/lcs'
-require 'diff/lcs/hunk'
-
module RSpec
- module Support
diff --git a/rspec-support/spec/rspec/support/encoded_string_spec.rb b/rspec-expectations/spec/rspec/expectations/encoded_string_spec.rb
index a9d4052..95b2f97 100644
--- a/rspec-support/spec/rspec/support/encoded_string_spec.rb
+++ b/rspec-expectations/spec/rspec/expectations/encoded_string_spec.rb
@@ -1,6 +1,4 @@
-require 'spec_helper'
-
-module RSpec::Support
+module RSpec::Expectations
describe EncodedString do
diff --git a/rspec-support/lib/rspec/support/encoded_string.rb b/rspec-expectations/lib/rspec/expectations/encoded_string.rb
index 654a208..eeb0b16 100644
--- a/rspec-support/lib/rspec/support/encoded_string.rb
+++ b/rspec-expectations/lib/rspec/expectations/encoded_string.rb
@@ -1,5 +1,5 @@
module RSpec
- module Support
+ module Expectations
# @private
class EncodedString
diff --git a/rspec-support/lib/rspec/support/differ.rb b/rspec-expectations/lib/rspec/expectations/diff_presenter.rb
index 26da8d8..dd6e95d 100644
--- a/rspec-support/lib/rspec/support/differ.rb
+++ b/rspec-expectations/lib/rspec/expectations/diff_presenter.rb
@@ -1,27 +1,14 @@
-RSpec::Support.require_rspec_support 'encoded_string'
-RSpec::Support.require_rspec_support 'hunk_generator'
-
+require 'diff/lcs'
+require 'diff/lcs/hunk'
diff --git a/rspec-support/spec/rspec/support/differ_spec.rb b/rspec-expectations/spec/rspec/expectations/diff_presenter_spec.rb
index 69b1237..0c19de4 100644
--- a/rspec-support/spec/rspec/support/differ_spec.rb
+++ b/rspec-expectations/spec/rspec/expectations/diff_presenter_spec.rb
@@ -1,18 +1,21 @@
# encoding: utf-8
-require 'spec_helper'
require 'ostruct'
module RSpec
--- rspec-expectations/lib/rspec/expectations/diff_presenter.rb 2014-03-23 23:31:14.214580118 -0700
+++ rspec-support/lib/rspec/support/diff_presenter.rb 2014-03-23 23:27:18.402592651 -0700
@@ -1,14 +1,26 @@
-require 'diff/lcs'
-require 'diff/lcs/hunk'
+require "rspec/support/encoded_string"
+require "rspec/support/differ"
require 'pp'
-RSpec::Support.require_rspec_expectations "encoded_string"
-RSpec::Support.require_rspec_expectations "differ"