Skip to content

Instantly share code, notes, and snippets.

@jenny-codes
jenny-codes / notes_upload.plist
Created May 10, 2019 13:56
Launchctl job to auto-upload to github
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>notes.upload</string>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>[your directory]</string>
#!/bin/bash
msg=${*:-'regular upload'}
git add .
git commit -m "$msg"
git push
exit 0
def missing_arguments(block)
block.call('arg1', 'arg2')
end
a_proc = Proc.new { |a, b, c| "a_proc outputs #{a} & #{b} & #{c}" }
a_lambda = lambda { |a, b, c| "a_lambda output #{a} & #{b} & #{c}" }
puts missing_arguments(a_proc)
puts missing_arguments(a_lambda)
def proc_return
Proc.new { return 'return from inside Proc' }.call
return 'return from proc function'
end
def lambda_return
lambda { return 'return from inside lambda' }.call
return 'return from lambda function'
end
# set up instances
# Since blocks cannot be stored in a variable, we create a method to inspect it.
def dummy_method(&a_block)
puts "A block is a #{a_block.class}"
puts "A block instance: #{a_block.inspect}"
end
a_proc = Proc.new { 'This is a proc' }
a_lambda = lambda { 'This is a lamnda' }
# test begins
# method definition
def run_variations(variation_options = {}, &block)
keys = variation_options.keys
values = variation_options.fetch_values(*keys)
variant_klass = Struct.new(*keys)
values.first
.product(*values[1..-1])
.each do |variation|
block.call(variant_klass.new(*variation))
@jenny-codes
jenny-codes / ruby_gil_mutex.rb
Last active March 5, 2019 13:34
Use Mutex to avoid involuntary context switch in Ruby.
def send_money(amount)
puts "Sending $#{amount}"
sleep 1 # Simulate network call sending of money
end
lock = Mutex.new
threads = []
money_is_sent = false
2.times do
@jenny-codes
jenny-codes / ruby_gil_threads_test.rb
Created March 5, 2019 13:14
At database querying the GIL is released.
require 'thwait'
require 'pg'
start = Time.now
first_sleep = Thread.new do
puts 'Starting sleep 1'
conn = PG::Connection.open(dbname: 'test')
conn.exec('SELECT pg_sleep(1);')
puts 'Finished sleep 1'
@jenny-codes
jenny-codes / ruby_benchmark_usage.rb
Last active February 19, 2019 05:50
Benchmarking performances between :preload, :eager_load, :includes
require 'benchmark/ips'
def test
Benchmark.ips do |bm|
bm.report('#preload ') do
Speaker.preload(:talks).to_a
end
bm.report('#eager_load') do
Speaker.eager_load(:talks).to_a
@jenny-codes
jenny-codes / activerecord_joins_usage.rb
Last active February 19, 2019 04:41
Examples of when to use :joins provided by Rails' ActiveRecord
# Speaker -> Talks
# CORRECT USAGE
# Select speakers that have one or more talks and returns all attributes (columns) plus talk's title.
Speaker.joins(:talks).select('distinct speakers.*, talks.title as talks_title').to_a
# SELECT distinct speakers.*, talks.title as talks_title FROM `speakers` INNER JOIN `talks` ON `talks`.`speaker_id` = `speakers`.`id`
# Select speaker whose associated talk(s) is at status 5.
Speaker.joins(:talks).where(talks: {status: 5})
# SELECT `speakers`.* FROM `speakers` INNER JOIN `talks` ON `talks`.`speaker_id` = `speakers`.`id` WHERE `talks`.`status` = 5