Skip to content

Instantly share code, notes, and snippets.

View moxley's full-sized avatar

Moxley Stratton moxley

View GitHub Profile
@moxley
moxley / file_read_irb.rb
Created April 16, 2014 22:44
Possible bug
# IO#read should return a string with ASCII-8BIT encoding
# http://www.ruby-doc.org/core-2.1.1/IO.html#method-i-read
irb(main):001:0> RUBY_VERSION
=> "2.1.1"
irb(main):002:0> File.write('test', 'abcdefg')
=> 7
irb(main):003:0> File.read('test').encoding
=> #<Encoding:UTF-8>
@moxley
moxley / tee_logger.rb
Last active August 29, 2015 14:00
Ruby Logger that logs to STDERR only in console, while always logging to the passed-in Ruby logger
# Always logs to passed-in logger
# Logs to $stderr when in console
class TeeLogger
def initialize(ruby_logger)
@logger = ruby_logger
end
private
def logger
@moxley
moxley / _form.html.slim
Created April 28, 2014 22:04
Parse this Slim 2.0 file
= simple_form_for [:admin, @basic_po], as: :purchase_order,
url: basic_po_admin_purchase_orders_path,
html: { class: 'form-horizontal' } do |f|
.form_controls
= f.input :vendor_id, label: 'Vendor Id', placeholder: '3231'
= f.input :brand_title, as: :select, label: 'Brand',
collection: UpcMaster.all_brand_titles
= f.input :terms, as: :select, label: 'Terms',
collection: Erp::PurchaseOrder::PO_TERMS.to_a
# spec:
it "calls TrackShipment#track_delivery for the PurchaseReturnShipment" do
track_shipment = instance_double('Ops::Returns::TrackShipment')
expect(track_shipment).to receive(:foo)
track_shipment_class = class_double('Ops::Returns::TrackShipment').as_stubbed_const
expect(track_shipment_class).to receive(:new).with(prs).and_return(track_shipment)
tracker.detect_events
end
@moxley
moxley / gist:ac4dc9776cdb5712c51f
Last active August 29, 2015 14:02
rspec-fire detecting whether method is defined
# Spec:
it "calls TrackShipment#track_delivery for the PurchaseReturnShipment" do
track_shipment_class = class_double('Ops::Returns::TrackShipment').as_stubbed_const
expect(track_shipment_class).to receive(:foo).with(prs)
tracker.detect_events
end
# Code under test
class Ops::Returns::TrackBatchShipments
@moxley
moxley / memory_test3.rb
Last active August 29, 2015 14:05
Test apparent ruby-vips memory leak
# Ruby VIPS Memory Test
# Instructions: `bundle exec ruby memory_test3.rb`
require 'vips'
def mem_usage
`ps -o rss -p #{$$}`.strip.split.last.to_i
end
include VIPS
@moxley
moxley / memory_test2.rb
Last active February 28, 2024 04:45
Test apparent memory leak in carrierwave/ruby-vips/vips
# Instructions: `bundle exec ruby memory_test2.rb`
# Variations:
# 1. `include CarrierWave::Vips` (default)
# 2. `include CarrierWave::MiniMagic` - first need to disable 'newrelic-carrierwave' gem
require 'carrierwave'
require 'carrierwave-vips'
# Memory test 2
@moxley
moxley / in_memory_arel_spec.rb
Last active August 29, 2015 14:05
In-memory Arel
# require 'spec_helper'
class InMemoryArel
attr_accessor :collection
def initialize(collection)
self.collection = collection
end
def all
househappy_test=# create table my_nulls (id integer not null, first_name character varying(255) default 'foo', last_name character varying(255) default 'bar');
CREATE TABLE
househappy_test=# create sequence my_nulls_id_seq start with 1 increment by 1 no minvalue no maxvalue cache 1;
CREATE SEQUENCE
househappy_test=# alter table only my_nulls alter column id set default nextval('my_nulls_id_seq'::regclass);
ALTER TABLE
househappy_test=# insert into my_nulls (first_name) values (null);
INSERT 0 1
househappy_test=# select * from my_nulls where first_name is null;
id | first_name | last_name
[1] website » class MyNull < ActiveRecord::Base
» end
=> nil
[2] website » my_null = MyNull.new
=> #<MyNull:0x007fc89cedd430> {
:id => nil,
:first_name => "foo",
:last_name => "bar"
}
[3] website » my_null.save!