Skip to content

Instantly share code, notes, and snippets.

View ifyouseewendy's full-sized avatar

Di Wen ifyouseewendy

View GitHub Profile
@ifyouseewendy
ifyouseewendy / ruby_trick.md
Created March 1, 2015 08:28
some snippets

How to check N is a power of 2 ?

def is_a_power_of_2?(n)
  puts "#{n} is a power of 2!" if n.bit_length != (n-1).bit_length
end
# SPOJ - PRIME1 http://www.spoj.com/problems/PRIME1/
#
# *NOTE*
#
# 1. Presieve the primes under sqrt(1_000_000_000).
# 2. Multiple the primes to flag numbers which are not primes.
# 3. Sieve in given range.
primes = [2]
@ifyouseewendy
ifyouseewendy / git_object_storage.rb
Last active August 29, 2015 14:12
A demo script writing Git blob object from ProGit
require 'digest/sha1'
require 'zlib'
require 'fileutils'
# put_raw_object("what is up, doc?", 'blob')
def put_raw_object(content, type)
# Generate SHA-1
header = "#{type} #{content.length}\0" # => "blob 16\000"
store = header + content # => "blob 16\000what is up, doc?"
sha1 = Digest::SHA1.hexdigest(store) # => "bd9dbf5aae1a3862dd1526723246b20206e5fc37"
@ifyouseewendy
ifyouseewendy / generate_stats.rb
Last active August 29, 2015 14:12
Update git history of author and email.
File.open("stats/stats_#{Time.now.to_i}.tsv","w") do |of|
lines = `git log --format=format:"%an\t%ae\t%ce"`.split("\n").uniq
stats = lines.reduce({}) do |ret, line|
author, a_email, c_email = line.strip.split("\t")
ret[author] ||= {}
ret[author][:a_email] ||= []
ret[author][:c_email] ||= []
ret[author][:a_email] << a_email unless ret[author][:a_email].include? a_email
ret[author][:c_email] << c_email unless ret[author][:c_email].include? c_email
ret
@ifyouseewendy
ifyouseewendy / git_rebase_demo.md
Created December 16, 2014 03:50
a git rebase demo from Pro Git

Now $git show-branch master server client shows like this:

<-- commit0 <-- commit1 <-- commit2                 master
                  \
                   \-- commit3 <-- commit4          server
                          \
                           \-- commit5 <-- commit6  client
@ifyouseewendy
ifyouseewendy / pry_workflow.rb
Last active August 29, 2015 14:10
pry workflow on server console
[34] pry(main)> edit -h
Usage: edit [--no-reload|--reload|--patch] [--line LINE] [--temp|--ex|FILE[:LINE]|OBJECT|--in N]
Open a text editor. When no FILE is given, edits the pry input buffer.
When a method/module/command is given, the code is opened in an editor.
Ensure `Pry.config.editor` is set to your editor of choice.
edit sample.rb edit -p MyClass#my_method
edit sample.rb --line 105 edit MyClass
edit MyClass#my_method edit --ex

Write a file named two-threads.rb.

t1 = Thread.new{ while true; end }
t2 = Thread.new{ while true; end }

t1.join
t2.join
@ifyouseewendy
ifyouseewendy / class_and_singleton_instance_variables.rb
Last active August 29, 2015 14:07
an example comparing *instance variable*, *class instance variable*, and *singleton class instance variable*.
class Foo
def i_method
@foo = ''
end
@bar = ''
class << self
@baz = ''
@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