Skip to content

Instantly share code, notes, and snippets.

@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
@ssrihari
ssrihari / clojure-learning-list.md
Last active April 24, 2024 03:06
An opinionated list of excellent Clojure learning materials

An opinionated list of excellent Clojure learning materials

These resources (articles, books, and videos) are useful when you're starting to learn the language, or when you're learning a specific part of the language. This an opinionated list, no doubt. I've compiled this list from writing and teaching Clojure over the last 10 years.

  • 🔴 Mandatory (for both beginners and intermediates)
  • 🟩 For beginners
  • 🟨 For intermediates

Table of contents

  1. Getting into the language
/**
* GlobalsDebugger
*
* Inspect the stack when a global variable is being set on the window object.
* Given a global variable name, it proxies the variable name in the window
* object adding some custom code that will be invoked whenever the variable
* is set. The custom code will log the current stack trace and halt the code
* execution to allow inspecting the stack and context in your browser DevTools.
* You can use the "globalsToInspect" query-parameter to set a comma-separated
* list of names of the variables you want to inspect.

Cheat sheet: modules

Named exports, named imports, namespace imports

If we put export in front of a named entity inside a module, it becomes a named export of that module. All other entities are private to the module.

//===== lib1.mjs =====
// Named exports
@Kukunin
Kukunin / ractors.rb
Last active December 28, 2023 12:53
Ruby Ractors vs Threads benchmark
require 'benchmark'
require 'etc'
Ractor.new { :warmup } if defined?(Ractor)
def fibonacci(n)
return n if (0..1).include? n
fibonacci(n - 1) + fibonacci(n - 2)
end
@swanson
swanson / .irbrc
Last active March 3, 2024 17:14
.irbrc color coding Rails console by environment
# Add color coding based on Rails environment for safety
if defined? Rails
banner = if Rails.env.production?
"\e[41;97;1m #{Rails.env} \e[0m "
else
"\e[42;97;1m #{Rails.env} \e[0m "
end
# Build a custom prompt

for-of, early termination, iterators, and generators

If we terminate a loop early, for-of invokes method .return() of the iterator. Generators return generator objects (that implement both the Iterable interface and the Iterator interface). If we invoke .return() on such an object, the generator is terminated. We can prevent that by overriding .return().

More information: https://exploringjs.com/es6/ch_iteration.html#sec_iteration-protocol-in-depth

Demonstration:

function logIter(iter) {
@dhh
dhh / Gemfile
Created June 24, 2020 22:23
HEY's Gemfile
ruby '2.7.1'
gem 'rails', github: 'rails/rails'
gem 'tzinfo-data', '>= 1.2016.7' # Don't rely on OSX/Linux timezone data
# Action Text
gem 'actiontext', github: 'basecamp/actiontext', ref: 'okra'
gem 'okra', github: 'basecamp/okra'
# Drivers
{
"env": {
"browser": true,
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:promise/recommended",
"plugin:sonarjs/recommended",
@dmshvetsov
dmshvetsov / worker_and_queue_perfect_pair.rb
Created February 5, 2019 05:33
Example of how we may use queue with threads to make efficient background worker.
class Worker
def self.start(num_threads:, queue_size:)
queue = SizedQueue.new(queue_size)
worker = new(num_threads: num_threads, queue: queue)
worker.spawn_threads
worker
end
def initialize(num_threads:, queue:)
@num_threads = num_threads