Skip to content

Instantly share code, notes, and snippets.

View emanuelhfarias's full-sized avatar

Emanuel H. Farias emanuelhfarias

View GitHub Profile
@v-kolesnikov
v-kolesnikov / app.rb
Created May 23, 2020 09:08
Single file Rails app
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'rails', '~> 6.0'
end
require 'action_controller/railtie'
@natematykiewicz
natematykiewicz / unroutable_routes.rake
Last active June 9, 2022 19:47
Find routes that will raise a routing error when requested
desc 'Find routes that will raise a routing error when requested'
task unroutable_routes: :environment do
# A lot of this code was taken from how `rake routes` works
# https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/railties/lib/rails/commands/routes/routes_command.rb
require 'action_dispatch/routing/inspector'
unroutables = Rails.application.routes.routes.
map { |r| ActionDispatch::Routing::RouteWrapper.new(r) }.
reject { |r| r.internal? || r.engine? || r.path.starts_with?('/rails/') || !r.controller }.
@andynu
andynu / show_method_history.rb
Created August 26, 2022 18:56
Given a ruby file and method name shows you all the different versions across the git history.
#!/usr/bin/env ruby
# Given a file and method_name
# Show all the different implementations across the git history (first commit per implementation).
#
# show_method_history <file> <method_name> --html
#
# e.g. show_method_history test/test_helper.rb sign_in --html
#
# WARNING: the --html output just dumps html files into your current folder.
#
@serradura
serradura / 01_factory_collection_filter.rb
Created February 23, 2023 17:22
Abstractions to filter an array of hashes in Ruby
class FactoryCollectionFilter
def initialize(config)
@config = config || {}
end
def call(filters: {}, data: [])
return data if filters.empty?
filters.reduce(data) do |filtered, (filter, value)|
filter_fn = @config[filter]
@kddnewton
kddnewton / json.rb
Last active August 7, 2023 19:54
Faster JSON parser in Ruby
# frozen_string_literal: true
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "json_pure"
gem "benchmark-ips"
end
@serradura
serradura / nano_case.rb
Last active March 30, 2023 12:36
NanoCase: Abstraction inspired by the u-case gem (compatible with old and new Rubies)
class NanoCase
class Result
UNDEFINED = :undefined
attr_reader :type, :data
def initialize(type, data)
@type = type
@data = data
end
Mix.install(
[
{:phoenix_playground, "~> 0.1.0"},
{:openai, "~> 0.6.1"}
],
config: [
openai: [
api_key: System.get_env("OPENAI_API_KEY"),
organization_key: System.get_env("OPENAI_ORGANIZATION_KEY")
]