Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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)