Skip to content

Instantly share code, notes, and snippets.

View fractaledmind's full-sized avatar

Stephen Margheim fractaledmind

View GitHub Profile
@brianhempel
brianhempel / bench_rails_memory_usage.rb
Last active March 25, 2025 18:43
A script to test the memory usage of your Rails application over time. It will run 30 requests against the specified action and report the final RSS. Choose the URL to hit on line 45 and then run with `ruby bench_rails_memory_usage.rb`.
require "net/http"
def start_server
# Remove the X to enable the parameters for tuning.
# These are the default values as of Ruby 2.2.0.
@child = spawn(<<-EOC.split.join(" "))
XRUBY_GC_HEAP_FREE_SLOTS=4096
XRUBY_GC_HEAP_INIT_SLOTS=10000
XRUBY_GC_HEAP_GROWTH_FACTOR=1.8
XRUBY_GC_HEAP_GROWTH_MAX_SLOTS=0
@Overbryd
Overbryd / 1_example.rb
Last active June 2, 2022 10:27
Sexiest assertion since I started testing APIs: assert_structure
# lets say this is the response we receive
response = {
"results" => {
"total_count" => 15,
"per_page" => 100,
"companies" => [
{
"company" => {
"name" => "Foo Bar Ltd",
"registered_address" => { ... },
@arjunvenkat
arjunvenkat / gist:1115bc41bf395a162084
Last active January 12, 2024 05:04
Seeding a Rails database with a CSV file

How to seed a Rails database with a CSV file

1. Setup

First, Create a folder inside of lib called seeds

Put your CSV file example.csv into the lib/seeds folder. In the example below, the file is called real_estate_transactions.csv

Make sure you've created a resource with the appropriate columns to match your seed data. The names don't have to match up.

@gaearon
gaearon / slim-redux.js
Last active December 3, 2024 06:34
Redux without the sanity checks in a single file. Don't use this, use normal Redux. :-)
function mapValues(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
result[key] = fn(obj[key], key);
return result;
}, {});
}
function pick(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
if (fn(obj[key])) {
# this is a dirty implementation of logger that
# compiles AR queries with trace into /last_request_log.html
# the snippet is useful when optimizing performance of the endpoint
class QueryLogSubscriber < ActiveSupport::LogSubscriber
TRACE_LEVEL = :app
LINES = 5
IGNORE_CACHED_QUERIES = false
def initialize
@wesbos
wesbos / tab-trigger.js
Created November 16, 2015 19:33
How to properly get a TAB trigger working with Emmet inside of JSX
{
"keys": ["tab"],
"command": "expand_abbreviation_by_tab",
// put comma-separated syntax selectors for which
// you want to expandEmmet abbreviations into "operand" key
// instead of SCOPE_SELECTOR.
// Examples: source.js, text.html - source
"context": [
{
@InsightForesight
InsightForesight / rb_snowflake_id.rb
Created November 20, 2015 15:46 — forked from pmarreck/rb_snowflake_id.rb
An implementation of Twitter's Snowflake ID generation algorithm in pure Ruby. Note: I didn't rewrite the bits that run this as a service. This is just the algorithm.
require 'monitor'
class IdWorker
attr_reader :worker_id, :datacenter_id, :reporter, :logger, :sequence, :last_timestamp
TWEPOCH = 1288834974657
WORKER_ID_BITS = 5
DATACENTER_ID_BITS = 5
MAX_WORKER_ID = (1 << WORKER_ID_BITS) - 1

Upload a file in Rails, parse it, then throw it away

You don't need Paperclip or Carrierwave or even an ActiveRecord model to manage file uploads in Rails if all you need to do is read the file in and extract data from it.

The Catalog class (referenced in the UploadController#create method) knows how to read a JSON file, translate and extract the data therein, and create or modify Book and Product records in the surrounding application.

By using the FileUtils.cp class method, we move the uploaded file into a known location at a known filename, so the Catalog (PORO) can do its work. The tempfile created by Rack during the upload is harvested as normal by garbage collection, but the copy we made is deleted manually after the parsing process is completed.

@lasley
lasley / Gemfile
Last active April 28, 2023 21:23
Sample Devise Implementation w/ OmniAuth
gem 'devise'
# ENV variable management
gem 'figaro'
# OmniAuth Authentication providers
gem 'omniauth-google-oauth2'
gem 'omniauth-facebook'
group :development, :test do
@wojteklu
wojteklu / clean_code.md
Last active May 15, 2025 14:30
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules