Skip to content

Instantly share code, notes, and snippets.

Avatar

Marc Chung mchung

View GitHub Profile
View instance-bm.rb
require 'benchmark/ips'
class SelfRuby
attr_accessor :foo
def initialize
self.foo = 1
end
end
# SLOWEST!
View yield-call-benchmark.rb
require 'benchmark/ips'
class A
def test
10.times do
yield
end
end
end
@mchung
mchung / poro-hashie-ostruct-benchmark.rb
Last active Aug 5, 2018 — forked from jgaskins/benchmark.rb
Hashie vs OpenStruct vs PORO performance
View poro-hashie-ostruct-benchmark.rb
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 Jan 4, 2017
TIL difference between RSpec's syntaxes for returning a mocked value.
View debugger.rb
(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
Protobuf: Compute Timestamp from current time in Ruby
View timestamp.proto
// 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 Oct 22, 2020
Example of callbacks with Python
View callback.py
# 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 Dec 31, 2015
Remote Roku Ruby controller
View roku.rb
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 Nov 12, 2014
Sinatra: How to use "settings" to inject dependencies
View boot.rb
# Setup the Service::Api
Service::Api.set(:environment, :production)
Service::Api.set(:root, Service.root)
Service::Api.set(:service, Service::Provider.new)
View ree-1.8.7-2011.03
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 ..
@mchung
mchung / sourcing.io.html
Created Feb 5, 2014
Preloading video trick
View sourcing.io.html
<div class="preview">
<div class="inner">
<img src="/assets/site/placeholder-4abfbbec6d239b5708837804f8149b32.png">
<video preload="auto">
<source src="https://di3zio5pem6rb.cloudfront.net/site/preview.mp4" type="video/mp4">
<source src="https://di3zio5pem6rb.cloudfront.net/site/preview.ogv" type="video/ogv">
<source src="https://di3zio5pem6rb.cloudfront.net/site/preview.webm" type="video/webm">
</video>
</div>
You can’t perform that action at this time.