Skip to content

Instantly share code, notes, and snippets.

View ifyouseewendy's full-sized avatar

Di Wen ifyouseewendy

View GitHub Profile
@ifyouseewendy
ifyouseewendy / faraday_em.rb
Created October 19, 2014 09:53
benchmark on faraday with net/http, em-http-request and typhoeus.
require 'benchmark'
require 'faraday'
require 'faraday_middleware'
class Wendi
def initialize(app)
@app = app
end
def call(env)
puts 'wendi requesting...'
@ifyouseewendy
ifyouseewendy / madeleine_example.rb
Last active August 29, 2015 14:07
example with Madeleine https://github.com/ghostganz/madeleine **Don't know how to restore from the snapshots yet**
require 'rubygems'
require 'madeleine'
class Employee
attr_accessor :name, :number, :address
def initialize(name, number, address)
@name = name
@number = number
@address = address
##
# Note from Ruby Pocket Reference
#
# Since constants refer to objects, the contents of the object
# to which the constant refers may change without Ruby generating
# a warning. Thus, Ruby constants are called *mutable*, because,
# although the constant is only expected to refer to a single
# object throughout the program, what's contained in that object
# may vary.
module MyMethods
def my_public_method
puts "Public"
end
protected
def my_protected_method
puts "Protected"
end
@ifyouseewendy
ifyouseewendy / subclasses_of.rb
Created October 8, 2014 14:19
Get subclasses, using Iterator Pattern
def subclasses_of(superclass)
subclasses = []
ObjectSpace.each_object(Class) do |k|
next if !k.ancestors.include?(superclass) || superclass == k || k.to_s.include?('::') || subclasses.include?(k.to_s)
subclasses << k.to_s
end
subclasses
end
@ifyouseewendy
ifyouseewendy / metal_and_base.md
Last active August 29, 2015 14:06
How to make method in module override the method in base class?
module A
  def foo
    puts 'foo from A'
    super
  end
end

module B
 def foo
@ifyouseewendy
ifyouseewendy / git-fu.md
Last active August 29, 2015 14:06
some useful commands

How to revert all the commits involved by a merge?

git revert {merge SHA} -m 1

ref -m, --mainline <n> parent number

eg.

➜  wendi-mac ~/tmp/demo (master)                                                                   (ruby-2.0.0-p353)
@ifyouseewendy
ifyouseewendy / config.auth.ru
Created September 23, 2014 08:05
a bundle of `config.ru`, testing rack middlewares.
use Rack::Auth::Basic, 'app' do |usr, pwd|
usr == 'wendi' && pwd == '123123'
end
# => Request Headers
# Authorization:Basic d2VuZGk6MTIzMTIz
# realm = 'Hello, wrold'
# opaque = '1234567890'
# use Rack::Auth::Digest::MD5, realm, opaque do |password|
@ifyouseewendy
ifyouseewendy / mongo-dump_and_restore.rb
Created May 23, 2014 02:47
A basic script to integrate `mongodump` and `mongorestore`.
#!/usr/bin/env ruby
require 'date'
require 'fileutils'
app_key = ''
DUMP_HOST = 'localhost'
DUMP_PORT = '27017'
@ifyouseewendy
ifyouseewendy / throw_and_catch.rb
Created May 19, 2014 16:08
throw and catch example
# catch executes its block.
# If a throw is executed, Ruby searches up its stack for a catch block with a tag corresponding to the throw's tag.
# If found, that block is terminated, and catch returns the value given to throw.
# If throw is not called, the block terminates normally, and the value of catch is the value of the last expression evaluated.
# catch expressions may be nested, and the throw call need not be in lexical scope.
def routine(n)
puts n
throw :done, "I'm the return value" if n <= 0
routine(n-1)