Skip to content

Instantly share code, notes, and snippets.

View ramfjord's full-sized avatar

Thomas Ramfjord ramfjord

  • Quantcast
  • San Francisco, CA
View GitHub Profile
rb_hash = {hello: 123}
system("curl -H 'Content-Type: application/json' -d '#{JSON.dump(rb_hash)}' '#{URL}'")
@ramfjord
ramfjord / MyParams.rb
Last active February 28, 2019 22:10
Abusing ActiveModel::Validations
class MyParameter::Base < OpenStruct
def initialize(name:, description:, validations: proc {})
@validator = Class.new(Struct.new(:value)) do
include ActiveModel::Validations
instance_eval(&validations)
def self.model_name()
ActiveModel::Name.new(self, nil, "temp")
end
end
end
@ramfjord
ramfjord / proc_class.rb
Created February 27, 2019 21:50
invoke included module from proc?
p = ->() { validates_presence_of :foo }
B = Class.new() do
include ActiveModel::Validations
p.call
end
# => NoMethodError: undefined method `validates_presence_of' for main:Object
@ramfjord
ramfjord / bad_help_message
Created February 20, 2019 01:49
bad_help_message
# bundle help list
Usage:
bundle show GEM [OPTIONS]
Options:
[--paths=List the paths of all gems that are required by your Gemfile.], [--no-paths]
[--outdated=Show verbose output including whether gems are outdated.], [--no-outdated]
[--no-color] # Disable colorization in output
-r, [--retry=NUM] # Specify the number of times you wish to attempt network commands
-V, [--verbose], [--no-verbose] # Enable verbose output mode
@ramfjord
ramfjord / index_usage_view.sql
Created February 15, 2019 01:30
Index usage query
DROP VIEW IF EXISTS index_usage;
CREATE OR REPLACE TEMP VIEW index_usage AS
SELECT pg_stat_user_tables.relname::TEXT AS table_name
, psui.idx_scan
, pg_size_pretty(pg_relation_size(indexrelid::REGCLASS::TEXT)) AS idx_size
, pg_relation_size(indexrelid::REGCLASS::TEXT) AS idx_bytes
, indkey AS idx_columns
, indisunique
, indexrelid::REGCLASS AS index
-- , relname::TEXT AS table_name
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '3.2.18' # Locked for compatibility
gem 'rake', '0.9.2.2'
gem "audited-activerecord", "3.0.0" # last version that supports rails 3
gem 'composite_primary_keys', '~> 5'
gem 'pg'
// https://play.golang.org/p/v5jbAvnHvL
package main
import (
"fmt"
)
type classBuild struct {
baseMin, baseMax int64
multipliers map[string]float64
// is there a more idiomatic way to check if the client is asking for JSON?
func isJson(r *http.Request) bool {
contentType, hasContentType := r.Header["Content-Type"]
return strings.HasSuffix(r.URL.Path, ".json") ||
(hasContentType && (contentType[0] == "application/json"))
}
@ramfjord
ramfjord / rspec_stub_with_block.rb
Created April 12, 2017 22:55
Rspec stub with block
# I can stub a method like this with rspec-mocks
allow(Math).to receive(:double) { |x| x * 2 }
expect(Math.double(3)).to eq(6)
# Is there an equivalent for Mocha?
@ramfjord
ramfjord / cards.rb
Last active September 7, 2016 22:33
Test strategies for Wayne's card game (description in comments)
# There are 7 cards strictly numbered from 1 to 7. There are 3 individuals: A,
# B, and C. A and B each have 3 of these cards, and C has just the final 1
# card. The problem is this... A wants to convey to B what his 3 cards are
# (with 100% certainty) in such a way that C is *never* 100% certain of them.
# There is no private pre-strategizing talk between A and B (in other words, C
# can know full well the algorithm/process and still not be able to figure out
# A's holdings).
#
# How can A accomplish this information transmission as described above?
#