Skip to content

Instantly share code, notes, and snippets.

View jaynetics's full-sized avatar

Janosch Müller jaynetics

View GitHub Profile
@jaynetics
jaynetics / script.rb
Created June 27, 2023 17:50
download all comments of a youtube video
#!/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}"
@jaynetics
jaynetics / benchmark.rb
Last active June 26, 2023 19:29
String#respond_to? vs ::method_defined?
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)
@jaynetics
jaynetics / access_db_matcher.rb
Created June 26, 2023 19:25
test in rspec that a block of code does / does not access the DB
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
@jaynetics
jaynetics / regexp_properties_by_ruby_version.rb
Created June 14, 2023 19:15
Show when each regexp unicode property was added to Ruby
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
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}
@jaynetics
jaynetics / struct2.rb
Created May 30, 2023 18:32
Attempt at overriding Struct subclass ::new in Ruby for better kwargs performance
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, *, **)
@jaynetics
jaynetics / benchmarks.rb
Last active May 25, 2023 12:00
Performance overhead of Ruby TracePoint API
######
# 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
@jaynetics
jaynetics / find_unused_routes.rake
Last active May 15, 2023 19:38
Find rails routes with no corresponding controller action
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
@jaynetics
jaynetics / find_unused_modules.rb
Created May 4, 2023 17:37
find possibly unused modules in a rails app
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 }
@jaynetics
jaynetics / hash_new_vs_dup.rb
Created February 27, 2023 10:52
Ruby benchmark for making empty, compare_by_identity Hash with new / literal vs dup
# 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: