Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / ar_lazy_preload_example.rb
Created June 21, 2022 18:47
ar_lazy_preload example (for graphql without N+1 issues)
# Config
require 'rails'
require 'ar_lazy_preload' # https://github.com/DmitryTsepelev/ar_lazy_preload
require 'active_record'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
# not sure where this is supposed to happen, but it's necessary b/c ar_lazy_preload
# adds its functionality in a callback after active record finishes loading.
ArLazyPreload::Railtie.config.to_prepare_blocks.each(&:call)
@JoshCheek
JoshCheek / active_record_comparing_different_ways_to_load_associated_data.rb
Created June 8, 2022 23:31
ActiveRecord: Comparing different ways to load associated data (`includes` vs `preload` vs `eager_load`)
# Config
require 'active_record'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
# Migrations
ActiveRecord::Schema.define do
self.verbose = false
create_table(:users) { |t| t.string :name }
create_table :posts do |t|
t.string :name