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
#!/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 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
random_strings = (1..100_000).map { ('a'..'z').to_a.shuffle[0, 8].join }; nil | |
Benchmark.bm do |x| | |
x.report { random_strings.each { |s| s.respond_to?(s) } } | |
x.report { random_strings.each { |s| String.method_defined?(s) } } | |
end | |
# => user system total real | |
# => 0.042596 0.001688 0.044284 ( 0.044286) | |
# => 0.011542 0.000012 0.011554 ( 0.011556) |
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
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 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 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
desc 'Finds unused partials (i.e. view files starting with "_"). '\ | |
'May find false positives (programmatically chosen partials).' | |
task :find_unused_partials do | |
UnusedPartialFinder.run | |
end | |
module UnusedPartialFinder | |
module_function | |
RENDER_CALL_REGEX = %r{\Wrender[^'"]*(['"])/?((?:(?!\1).)+)\1} |
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
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 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 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
desc 'Finds unused routes (routes with no corresponding controller action)' | |
task find_unused_routes: :environment do | |
checked_actions = %i[create destroy edit index new show update] | |
Rails.application.eager_load! | |
endpoints = Rails.application.routes.routes.map do |route| | |
ActionDispatch::Routing::RouteWrapper.new(route).endpoint | |
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
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 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: |
NewerOlder