Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / most_allocated_objects.rb
Last active October 16, 2022 09:31
Finding locations of most allocated objects
require 'objspace'
def self.show_allocations(&block)
_ = ObjectSpace.trace_object_allocations &block
ObjectSpace
.each_object
.to_a
.filter_map do |obj|
file = ObjectSpace.allocation_sourcefile obj
line = ObjectSpace.allocation_sourceline obj
@JoshCheek
JoshCheek / contains.rb
Created October 11, 2022 00:30
Checks if the elements of one array are contained by another
# https://twitter.com/postmodern_mod3/status/1579281378390478849
def contains?(superary, subary)
(0..superary.length-subary.length).any? do |start|
subary.each_index.all? do |offset|
superary[start+offset] == subary[offset]
end
end
end
# true
@JoshCheek
JoshCheek / active_record_query_ordering.rb
Created October 4, 2022 10:32
Experiment on ordering an active record query
require 'active_record'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = false
create_table :posts do |t|
t.string :title
t.string :status
end
@JoshCheek
JoshCheek / finding_where_objs_come_from.rb
Created October 6, 2022 13:13
Figuring out where objects are coming from
# followup for https://twitter.com/kirill_shevch/status/1577930057728856064
require 'objspace'
require 'json'
def some_code
[ 10_000.times.map { "hi" },
5_000.times.map { "there" },
JSON.load('{ "omg": 123, "wtf": true, "bbq": 23.45 }'),
]
end
@JoshCheek
JoshCheek / example_spec.rb
Last active September 29, 2022 17:19
Example code for twitter comment I made about making tests less tedious
# https://twitter.com/josh_cheek/status/1575535820945211394
require 'rspec/autorun'
def idk(source)
tag, args = source.split(" ", 2)
[ "@_target",
%("<#{tag}>"),
"_output(#{args})",
%("</#{tag}>"),
].join(" << ")
@JoshCheek
JoshCheek / pg_cursor_example.rb
Created October 6, 2017 19:02
PostgreSQL cursor example
require 'pg'
db = PG.connect dbname: 'postgres'
db.exec("DROP DATABASE IF EXISTS just_fkn_around;")
db.exec("CREATE DATABASE just_fkn_around;")
db = PG.connect dbname: 'just_fkn_around'
define_method(:sql) { |sql| db.exec(sql).to_a }
sql <<-SQL
-- some data to query
@JoshCheek
JoshCheek / sib_render_markdown.rb
Last active September 14, 2022 01:16
Evaluating and updating annotations in ruby code in markdown files
require 'redcarpet' # gem
require 'open3' # stdlib
class SibRenderer < Redcarpet::Render::Safe
def block_code(code, language)
if language == "ruby"
code, _status = Open3.capture2("seeing_is_believing", "--xmpfilter-style", stdin_data: code)
end
super code, language # prob what you really want, see somefile.correctly_escaped.html
%(<pre><code class="#{language}">\n#{code}</code></pre>) # better for this example, see somefile.from_example.html
end
@JoshCheek
JoshCheek / memoizing_toggles_with_a_weakmap.rb
Created July 18, 2022 16:04
Just an idea about how to memoize some calculations
class MemoizedToggles
attr_reader :memoizations
def initialize(&constructor)
@constructor = constructor
@memoizations = ObjectSpace::WeakMap.new
end
def enabled?(user, toggle)
@memoizations[user] ||= Hash.new do |hash, key|
@JoshCheek
JoshCheek / threadsafe_stubbing_example.rb
Created July 12, 2022 19:14
Example how how stubbing methods could be threadsafe (IDK if this is how RSpec does it or not)
# really rudimentary stubbing mechanism (eg doesn't support unstubbing)
def stub(obj, method, *expected, result)
Thread.current[:stubs] ||= {}
Thread.current[:stubs][method] ||= {}
Thread.current[:stubs][method][expected] = result
obj.define_singleton_method method do |*actual| # we'll ignore keyword args here for simplicity
Thread.current[:stubs][method].fetch(actual) do
raise "#{method} invoked with unexpected args: #{actual.inspect}"
end
end
@JoshCheek
JoshCheek / rack_test_example.rb
Created July 21, 2014 21:35
example of how to use rack test with minitest
require 'sinatra/base'
require 'minitest/autorun'
require 'minitest/spec'
require "rack/test"
require 'nokogiri'
class MyApp < Sinatra::Base
enable :inline_templates
get '/users/:id' do