Skip to content

Instantly share code, notes, and snippets.

DO
$$
DECLARE
s name;
t name;
rowc bigint;
BEGIN
FOR s, t IN select table_schema, table_name
from information_schema.tables
require 'benchmark/ips'
class SelfRuby
attr_accessor :foo
def initialize
self.foo = 1
end
end
# SLOWEST!
require 'benchmark/ips'
class A
def test
10.times do
yield
end
end
end
@mchung
mchung / poro-hashie-ostruct-benchmark.rb
Last active August 5, 2018 23:14 — forked from jgaskins/benchmark.rb
Hashie vs OpenStruct vs PORO performance
require 'hashie'
require 'ostruct'
require 'benchmark/ips'
class PORO
attr_reader :foo, :bar, :baz
def initialize attrs = {}
attrs.each do |attr, value|
instance_variable_set "@#{attr}", value
@mchung
mchung / debugger.rb
Last active January 4, 2017 06:32
TIL difference between RSpec's syntaxes for returning a mocked value.
(byebug) hello = instance_double("Planet")
#<InstanceDouble(Planet) (anonymous)>
# Using RSpec block syntax to specific a return value.
(byebug) allow(hello).to receive(:world) { "hello, world" }
#<RSpec::Mocks::VerifyingMessageExpectation #<InstanceDouble(Planet) (anonymous)>.world(any arguments)>
@mchung
mchung / timestamp.proto
Created May 20, 2016 07:41
Protobuf: Compute Timestamp from current time in Ruby
// Example lifted from https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto
//
// Example 6: Compute Timestamp from current time in Ruby
//
// [1] pry(main)> now = Time.now
// => 2016-05-20 00:33:04 -0700
// [2] pry(main)> seconds = now.to_i
// => 1463729584
// [3] pry(main)> nanos = now.nsec
// => 204053000
@mchung
mchung / callback.py
Last active April 2, 2021 08:00
Example of callbacks with Python
# Example of using callbacks with Python
#
# To run this code
# 1. Copy the content into a file called `callback.py`
# 2. Open Terminal and type: `python /path/to/callback.py`
# 3. Enter
def add(numbers, callback):
results = []
for i in numbers:
@mchung
mchung / roku.rb
Last active December 31, 2015 07:38
Remote Roku Ruby controller
require "io/console"
require "socket"
require "net/http"
require "uri"
require 'cgi'
SSDP_ADDR = "239.255.255.250";
SSDP_PORT = 1900;
SSDP_ST = "roku:ecp";
@mchung
mchung / boot.rb
Created November 12, 2014 17:19
Sinatra: How to use "settings" to inject dependencies
# Setup the Service::Api
Service::Api.set(:environment, :production)
Service::Api.set(:root, Service.root)
Service::Api.set(:service, Service::Provider.new)
build_package_patched() {
# These three patches are included when RVM builds REE
cd source
wget 'https://github.com/wayneeseguin/rvm/raw/master/patches/ree/1.8.7/tcmalloc.patch'
wget 'https://github.com/wayneeseguin/rvm/raw/master/patches/ree/1.8.7/stdout-rouge-fix.patch'
wget 'https://github.com/wayneeseguin/rvm/raw/master/patches/ree/1.8.7/no_sslv2.diff'
patch -p1 < tcmalloc.patch
patch -p1 < stdout-rouge-fix.patch
patch -p1 < no_sslv2.diff
cd ..