Instantly share code, notes, and snippets.

View .gitignore
A-small-practice.out.txt
A-large-practice.out.txt
View ar_pg_enum.rb
# require 'pg'; PG.connect(dbname: 'postgres').exec('create database josh_testing') # <-- uncomment to create the db (only needs to run once)
# ===== Setup ActiveRecord =====
require 'active_record'
require 'logger'
ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'josh_testing'
ActiveRecord::Base.logger = Logger.new $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
# ===== Migration =====
View strong_params_bugs.rb
# Strong params tries to require all the files they depend on.
# This way you can require strong params itself, without having the entire world loaded
`gem which action_controller/metal/strong_parameters | xargs ruby -ne 'print if /^require/'`
# => "require \"active_support/core_ext/hash/indifferent_access\"\n" +
# "require \"active_support/core_ext/hash/transform_values\"\n" +
# "require \"active_support/core_ext/array/wrap\"\n" +
# "require \"active_support/core_ext/string/filters\"\n" +
# "require \"active_support/core_ext/object/to_query\"\n" +
# "require \"active_support/rescuable\"\n" +
# "require \"action_dispatch/http/upload\"\n" +
View spline.rb
# Based on this video: https://www.youtube.com/watch?v=dxvmafuP9Wk
# Screenshot: https://twitter.com/josh_cheek/status/1049913914585165826
# Animation: https://twitter.com/josh_cheek/status/1050054519911063552
require 'graphics'
require 'matrix'
class Spline < Graphics::Simulation
def initialize(points, original_fn)
super 800, 600
@points = points
View rails_before_filters.rb
require "rails"
require 'action_controller/railtie'
# ===== Configuration =====
Rails.logger = Logger.new $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
class MahApp < Rails::Application
config.cache_classes = true
config.eager_load = false
View try_throw_vs_raise_rescue.rb
RUBY_ENGINE # => "ruby"
RUBY_VERSION # => "2.5.1"
RUBY_PLATFORM # => "x86_64-darwin17"
# QUESTION
#
# Say you want to bail from a web controller as soon as some method is called
# Eg in Rails, maybe you want a `render!` method, which is the same as render,
# but it also returns from the method that called it. You could accomplish this
View test.css
body {
color: blue;
}
View assert_db_query_count_test_helper_for_active_record.rb
require 'active_record'
ActiveRecord::VERSION::STRING # => "5.1.6"
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = false
create_table(:users) do |t|
t.string :name
t.boolean :activated, default: false
end
View counter.js
let count = 0
module.exports = () => count++
View integer_id_to_lsug.rb
# Note that I haven't read about the difference between the base 64 RFCs
def to_slug(id)
ber_id = [id].pack('w') # => "\x85\xC8_"
b64_id = [ber_id].pack('m') # => "hchf\n"
b64_id[/\A(.*?)=*\n*\z/, 1] # => "hchf"
end # => :to_slug
def from_slug(slug)
slug.unpack('m') # => ["\x85\xC8_"]
.first # => "\x85\xC8_"