Skip to content

Instantly share code, notes, and snippets.

@joemasilotti
joemasilotti / Endpoint.swift
Last active May 28, 2023 16:54
A Rails-like environment helper for iOS apps, from https://masilotti.com/rails-like-endpoint-switcher-for-ios-apps/
import Foundation
enum Environment: String {
case development, staging, production
}
extension Environment {
static var current: Environment {
if isAppStore {
return .production
@kconner
kconner / macOS Internals.md
Last active May 6, 2024 22:20
macOS Internals

macOS Internals

Understand your Mac and iPhone more deeply by tracing the evolution of Mac OS X from prelease to Swift. John Siracusa delivers the details.

Starting Points

How to use this gist

You've got two main options:

@dcyoung-dev
dcyoung-dev / active_link_to.rb
Last active April 10, 2023 16:54
An `active_link_to` helper that adds the `.active` class and sets `aria-current="page"`
@henrik
henrik / rspec_stub_env.rb
Last active July 26, 2023 14:48
Stub ENVs in RSpec.
def stub_env(key, value)
is_set_up_flag = "__stub_env_is_set_up"
unless ENV[is_set_up_flag]
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:[]).with(is_set_up_flag).and_return(true)
end
@postmodern
postmodern / benchmark.rb
Created February 23, 2023 02:23
Micro-benchmark for `value != nil` vs. `!value.nil?`
#!/usr/bin/env ruby
require 'benchmark'
Benchmark.bm do |b|
n = 1_000_000
value1 = 1
value2 = nil
#!/usr/bin/env bash
# Useful when you want to git bisect in a rails app and know
# you need to `bin/rake db:migrate VERSION="SOMETHING"` before
# you check out the next commit so the database is in the right
# state, but you don't know what SOMETHING is.
# Usage:
#
# $ migration_version_at_ref <REF>
@ali-p-q
ali-p-q / definitions.rb
Created December 31, 2022 13:32
Definitions file for solargraph-rails adding all methods from ActiveModel and Ruby's standard library
# The following comments fill some of the gaps in Solargraph's understanding of
# Rails apps. Since they're all in YARD, they get mapped in Solargraph but
# ignored at runtime.
#
# You can put this file anywhere in the project, as long as it gets included in
# the workspace maps. It's recommended that you keep it in a standalone file
# instead of pasting it into an existing one.
#
# @!parse
# class ActionController::Base
class Cond
IDENTITY = -> v { v }
def initialize(if_cond: IDENTITY, then_branch: IDENTITY, else_branch: IDENTITY)
@if_cond = if_cond
@then_branch = then_branch
@else_branch = else_branch
end
def call(v)
@mudge
mudge / api_controller.rb
Last active March 24, 2023 05:27
Instruct Rails to respect the Accept header even if it contains a wildcard
class ApiController < ApplicationController
before_action :only_respect_accept_header
private
# By default, Rails will ignore the Accept header if it contains a wildcard
# and assume the client wants HTML (or JS if using XMLHttpRequest). See
# https://github.com/rails/rails/blob/a807a4f4f95798616a2a85856f77fdfc48da4832/actionpack/lib/action_dispatch/http/mime_negotiation.rb#L171-L173
#
# If you don't expect your clients to be browsers, we want to override this
@kaspth
kaspth / routes.rb
Last active April 6, 2023 16:57
`draw` method to explore routes in the console
# All these requires are just for running via `irb`, if using `bin/rails console` you probably just need the method.
require "active_support/all" # Got an inflector NoMethodError, so I'm just being lazy here.
require "action_dispatch"
require "action_dispatch/routing/route_set"
require "action_dispatch/routing/inspector"
require "action_controller" # For the ActionController::Parameters autoload, which any route helper uses.
# Console helper play around with the routing DSL and tweak an individual route you're building.