This file contains hidden or 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
# This is a copy of the tabs component from AA 4.0.0.beta15. It might break in the future. | |
# Maybe an alternative will emerge in https://github.com/activeadmin/activeadmin/pull/8762 | |
# The class name does not matter. Put this in an initializer, or in app/lib & require manually. | |
class ActiveAdminTabs < ActiveAdmin::Component | |
builder_method :tabs | |
def tab(title, options = {}, &block) | |
title = title.to_s.titleize if title.is_a? Symbol | |
@menu << build_menu_item(title, options, &block) | |
@tabs_content << build_content_item(title, options, &block) |
This file contains hidden or 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
#!/usr/bin/env ruby | |
# usage: script.rb <video_id> <api_key> | |
require 'json' | |
require 'net/http' | |
video_id = ARGV[0].tap { |s| s&.length&.<(13) || fail('pass video id as first argument') } | |
key = ARGV[1].tap { |s| s&.length&.>(13) || fail('pass api key as second argument') } | |
url = "https://youtube.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId=#{video_id}&key=#{key}" |
This file contains hidden or 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
RSpec::Matchers.define :access_db do | |
match do |actual| | |
# track db access via ActiveRecord instrumentation | |
sql_callback = ->(*, event) do | |
# ignore automatic schema queries and transactions, though | |
@db_event = event unless event[:sql] =~ / a\.|max_ident|SAVEPOINT/ | |
end | |
ActiveSupport::Notifications.subscribed(sql_callback, 'sql.active_record') do | |
actual.call |
This file contains hidden or 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 'regexp_parser' | |
map = Regexp::Syntax.specified_versions.to_h do |ver| | |
[ver, Regexp::Syntax.const_get(ver).added_features[:property].to_a] | |
end | |
Regexp::Syntax::CURRENT.features[:property].to_h do |prop| | |
[prop, map.keys.find { |v| map[v]additions.include?(prop) }] | |
end |
This file contains hidden or 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
Struct2 = Class.new(Struct) | |
Struct2.singleton_class.prepend Module.new { | |
def new(...) | |
st = super | |
st.instance_eval <<~RUBY | |
alias __orig_new__ new | |
ARG_NOT_GIVEN = Object.new.freeze | |
def new(#{st.members[0]} = ARG_NOT_GIVEN, *, **) |
This file contains hidden or 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
###### | |
# Simplest case - one method that does nothing, with and without TP | |
###### | |
require 'benchmark/ips' | |
def noop; end | |
Benchmark.ips { |x| x.report('basic', 'noop') }; 1 |
This file contains hidden or 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
code = Dir['{app,config,lib}/**/*.{rb,slim,erb,rake}'].map { File.read _1 }.join; 1 | |
defs = code.scan(/(?:class|module) \K[A-Z][\w:]+(?=\n)/); 1 | |
refs = code.scan(/(?<!class |module )\b#{Regexp.union(defs + defs.map { |d| d.split('::').last })}\b/); 1 | |
unused = defs.uniq.sort.select { |d| refs.count(d) == 0 && refs.count(d.split('::').last) == 0 } |
This file contains hidden or 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
# results on ruby 3.2: | |
# | |
# Warming up -------------------------------------- | |
# new 277.194k i/100ms | |
# dup 394.839k i/100ms | |
# Calculating ------------------------------------- | |
# new 2.754M (± 1.9%) i/s - 13.860M in 5.034959s | |
# dup 4.006M (± 0.8%) i/s - 20.137M in 5.026561s | |
# | |
# Comparison: |
This file contains hidden or 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
# results on Ruby 3.2: | |
# | |
# Warming up -------------------------------------- | |
# hash 1.910M i/100ms | |
# case 1.297M i/100ms | |
# Calculating ------------------------------------- | |
# hash 19.029M (± 1.5%) i/s - 95.507M in 5.020123s | |
# case 12.604M (± 0.4%) i/s - 63.534M in 5.040776s | |
# | |
# Comparison: |
This file contains hidden or 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
# This allows doing | |
# | |
# Foo.prepend(WeakRefRegistry) | |
# | |
# foo = Foo.new # => #<Foo:0x1> | |
# foo_id = foo.object_id # => 1234 | |
# Foo.registry.fetch(foo_id) # => #<Foo:0x1> | |
# | |
# I.e. retrieve Foo instances by their object_id, | |
# without affecting garbage collection. |
NewerOlder