This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://twitter.com/josh_cheek/status/1575535820945211394 | |
require 'rspec/autorun' | |
def idk(source) | |
tag, args = source.split(" ", 2) | |
[ "@_target", | |
%("<#{tag}>"), | |
"_output(#{args})", | |
%("</#{tag}>"), | |
].join(" << ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |