Skip to content

Instantly share code, notes, and snippets.

View ifyouseewendy's full-sized avatar

Di Wen ifyouseewendy

View GitHub Profile
##
# 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)
@ifyouseewendy
ifyouseewendy / enumerator-lazy-benchmark.rb
Created May 16, 2014 11:03
Does Enumerator::Lazy make loops running faster? Maybe, It depends.
# ruby 2.1.2
require 'benchmark/ips'
Benchmark.ips do |x|
x.report { (1..100).select { |x| x % 3 == 0 }.select { |x| x % 4 == 0 } }
x.report { (1..100).select { |x| x % 3 == 0 && x % 4 == 0 } }
x.report { (1..100).lazy.select { |x| x % 3 == 0 }.select { |x| x % 4 == 0 }.to_a }
end
# http://ruby-doc.org/core-2.0/Proc.html#method-i-curry
#
# curry → a_proc
#
# Returns a curried proc.
#
# curry(arity) → a_proc
#
# If the optional arity argument is given, it determines the number of arguments.
#