Skip to content

Instantly share code, notes, and snippets.

@jorgemanrubia
jorgemanrubia / sync_hey_world_posts_with_jekyll.rb
Created January 25, 2023 08:39
Script I use to sync my HEY World posts with my Jekyll-powered personal site
require "rss"
require "ostruct"
RSS_FEED_URL = "https://world.hey.com/jorge/feed.atom"
def sync_posts
feed = RSS::Parser.parse(RSS_FEED_URL, false)
feed.items.each do |item|
post = OpenStruct.new title: item.title.content, date: item.published.content.strftime("%Y-%m-%d"), link: item.link.href
@jorgemanrubia
jorgemanrubia / measure.rb
Last active November 7, 2022 15:34
Script to measure and compare speed of running tests in parallel versus sequentially
require "benchmark"
# https://github.com/rails/rails/pull/42761
class Execution
TEST_FILE_PATH = "test/threshold_test.rb"
SINGLE_TEST_DURATION = 0.1
attr_reader :threshold, :parallel
def initialize(threshold, parallel: false)
@jorgemanrubia
jorgemanrubia / count_from_table_stats.rb
Created April 22, 2021 14:41
Estimated count from table stats (MySQL)
def count_from_table_stats(klass)
result = klass.connection.execute("show table status like '#{klass.table_name}'")
result.first[result.fields.index("Rows")]
end
module DisableReloaderWhileRunningTests
extend ActiveSupport::Concern
included do
setup do
@reloader_check = Rails.application.reloader.check
Rails.application.reloader.check = -> { false}
end
teardown do
@jorgemanrubia
jorgemanrubia / performance_helpers.rb
Last active May 1, 2020 09:22
Minitest helper to compare code with benchmark-ips
require 'benchmark/ips'
module PerformanceHelpers
BENCHMARK_DURATION = 1
BENCHMARK_WARMUP = 1
BASELINE_LABEL = "Baseline"
CODE_TO_TEST_LABEL = "Code"
# Usage:
@jorgemanrubia
jorgemanrubia / enumerators_performance.rb
Last active May 4, 2017 06:55
Ruby enumerators performance
require 'benchmark/ips'
COUNT = 500000
Benchmark.ips do |x|
x.report('Enumerable') do
total = 0
COUNT.times do |i|
total += i
@jorgemanrubia
jorgemanrubia / converter.rb
Created October 4, 2015 09:03
Script to convert reading list in csv format
require 'csv'
require 'date'
require 'ostruct'
require 'open-uri'
require 'fileutils'
class BooksConverter
OUTPUT_FOLDER = "output"
@jorgemanrubia
jorgemanrubia / discourse_users_merger.rb
Last active April 18, 2022 12:53
Script for merging existing discourse users
module DiscourseUsersMerger
class Merger
def merge(target_username, source_username)
raise "User to merge and target user can't be the same" if target_username == source_username
target_user = User.find_by_username!(target_username)
source_user = User.find_by_username!(source_username)
puts "About to merge #{source_username} (#{source_user.email}) into #{target_username} (#{target_user.email})"
puts "#{source_user.topics.count} topics with #{source_user.posts.count} posts will be moved. Ok to continue? (y/n)"
continue = STDIN.gets.chomp.downcase == 'y'
@jorgemanrubia
jorgemanrubia / bundler.rb
Last active December 10, 2015 04:18
Exclude gems in heroku Cedar. Heroku will ignore :development and :test by default, but not other groups (such as :cucumber). Previous Bamboo stack let you define BUNDLE_WITHOUT, but it doesn't work anymore on Cedar.
# Put this code in some file and require it at the beginning of your Gemfile
# Then you can list the groups to exclude from heroku. E.g:
#
# exclude_in_heroku :cucumber, :development, :test
#
# Based on this post by Matt Campbell: http://soupmatt.com/fixing-bundlewithout-on-heroku-cedar,
# which is based in an idea by @grosser. All the credits to them.
module Bundler
class Dsl
old_group = instance_method(:group)