Skip to content

Instantly share code, notes, and snippets.

@joshpencheon
joshpencheon / default_arguments.rb
Last active September 8, 2021 11:08
Demonstrating how default arguments are evaluated just-in-time
class Counter
attr_reader :thing
def initialize(thing)
@thing = thing
end
# The `number` argument's default is an expression that
# the will be evaluated in the context of the instance.
def print(number: thing.length)
@joshpencheon
joshpencheon / README.md
Last active August 23, 2021 11:16
A quick demo of using alias_method to refer to old versions of a method

Background

The state_machines gem allows you to conditionally define methods based on particular states (i.e. values of an attribute):

class Vehicle
  state_machine :alarm_state do
    state :active do
      def disable_alarm!
      end
@joshpencheon
joshpencheon / Dockerfile
Last active February 11, 2021 17:45
Run R-Shiny (R v4.0+) in a container, not as root
FROM rocker/shiny:4.0.3
ENV RENV_VERSION 0.12.5-2
RUN \
# Ensure shiny-server is run as the shiny user:
sed -i 's/exec/exec s6-setuidgid shiny/g' /etc/services.d/shiny-server/run && \
# Remove all the examples:
rm -r /srv/shiny-server && \
# Install Renv:
@joshpencheon
joshpencheon / implicit_parquet_schema.rb
Last active February 25, 2021 17:29
Ruby Parquet implicit schema
require 'active_support/all'
require 'parquet' # with CentOS7 dependencies as per https://arrow.apache.org/install/
data = {
'a_string' => ['hello world'],
'an_int' => [1],
'a_float' => [1.23],
'a_date' => [Date.today],
'a_time' => [Time.now.change(usec: 0)] # there's a precision limit...
}
@joshpencheon
joshpencheon / oracle_delete_all_shim.rb
Last active July 21, 2020 10:20
Hacky initialiser workaround to support bulk deletes on Oracle/Rails 6 with default orders
# Added to support Oracle on Rails 6.
# See: https://github.com/rsim/oracle-enhanced/issues/2023
original = ActiveRecord::Relation.instance_method(:delete_all).source
unless Digest::MD5.hexdigest(original) == '89a3e7c95873d278263c3d288a6cc242'
raise 'Please re-verify oracle_delete_all_shim.rb - method appears to have changed!'
end
module ActiveRecord
@joshpencheon
joshpencheon / has_changes_to_save.rb
Last active July 31, 2019 14:39
Debug script for a behaviour anomaly with has_changes_to_save? after a record fails to save (gist is formatting weirdly...)
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
# gem "rails", "5.2.2.1" # FAILS
# gem "sqlite3", "~> 1.3.6"
@joshpencheon
joshpencheon / rerun.rb
Created July 16, 2019 10:08
Proof-of-concept for rerunning flakey integration tests
ActionDispatch::IntegrationTest.class_eval do
# Attempts a test up to 3x before allowing it to fail / error.
def run
attempts_remaining = 3
previous_failure = failures.last
flakey_failures = []
loop do
break if attempts_remaining < 1
@joshpencheon
joshpencheon / test_outer_join_issue.rb
Created May 16, 2019 09:47
Test case for regression in merging left joins in Rails 6.0.0rc1
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
# gem "rails", "5.2.3" # WORKS
# gem "sqlite3"
@joshpencheon
joshpencheon / tensorflow.bzl.diff
Created March 1, 2019 15:54
Building Tensorflow r1.12 from source on CentOS7: undefined reference to `clock_gettime'
diff --git a/tensorflow/tensorflow.bzl b/tensorflow/tensorflow.bzl
index cad5de1..1cfcbbd 100644
--- a/tensorflow/tensorflow.bzl
+++ b/tensorflow/tensorflow.bzl
@@ -367,7 +367,7 @@ def tf_cc_shared_object(
srcs = [],
deps = [],
data = [],
- linkopts = [],
+ linkopts = ['-lrt'],
@joshpencheon
joshpencheon / wall.html
Created February 26, 2018 20:50
Help me hang a painting or eighteen.
<style type="text/css" media="screen">
div {
position: absolute;
top: 0px;
left: 0px;
background-color: black;
background-blend-mode: multiply;
opacity: 0.1;
}