Skip to content

Instantly share code, notes, and snippets.

View lulalala's full-sized avatar

lulalala lulalala

View GitHub Profile
@lulalala
lulalala / hash key benchmark
Created October 14, 2013 08:42
Use object_id as hash key can be 20x faster
ary= []
30.times do |i|
ary << {a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8}
end
Benchmark.bmbm do |x|
x.report("hash as key") do
hash = {}
ary.each do|a| hash[a] = 1 end
@lulalala
lulalala / splat_benchmark.rb
Created October 16, 2013 08:48
Using splat to accept large array is slow.
require 'benchmark'
iterations = 100_000
def a(*args)
args.size
end
def b(args)
args.size
@lulalala
lulalala / ffmpeg_compile.sh
Created October 17, 2013 16:33
compile options for minial ffmpeg which can encode/decode from images to h264
# for x264
./configure --enable-static \
--disable-opencl \
--disable-avs \
--disable-cli \
--disable-ffms \
--disable-gpac \
--disable-lavf \
--disable-swscale \
--prefix=/usr/local/
@lulalala
lulalala / original.html
Created February 7, 2014 03:50
Nokogiri 1.5.11 wbr element parsing. As you can see the "text after" got removed in the parsed.html because there are too many nested wbr elements.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://ogp.me/ns/fb#">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>1999</title>
</head>
<body>
<br><br><br>
text before
<p>
@lulalala
lulalala / calendar
Created September 3, 2014 06:13
SimpleCalendar styling
<table class="calendar"><thead><tr><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th><th>Sun</th></tr></thead><tbody><tr><td class="day past current-month wday-1">09/01
</td><td class="day past current-month wday-2">09/02
</td><td class="day today current-month wday-3"><a href="">09/03</a>
</td><td class="day future current-month wday-4">09/04
</td><td class="day future current-month wday-5">09/05
</td><td class="day future current-month wday-6">09/06
</td><td class="day future current-month wday-0">09/07
</td></tr><tr><td class="day future current-month wday-1">09/08
</td><td class="day future current-month wday-2">09/09
</td><td class="day future current-month wday-3">09/10
@lulalala
lulalala / gist:6be104641bcb60f9d0e8
Created November 11, 2014 09:30
RedirectResolver
require 'uri'
require 'net/http'
class RedirectResolver
def self.resolve(uri)
uri_input = uri
if uri.is_a? String
uri = URI.parse(uri)
end
@lulalala
lulalala / Controller Action.rb
Last active August 29, 2015 14:09
Payment Notification
# in OrdersController
def payment_notify
l = PaymentNotification.new
l.order_id = params[:id]
l.payment_method_id = params[:payment_method_id]
l.url = request.fullpath
l.raw_post = request.raw_post
l.data = {remote_ip:request.remote_ip}
module Global
def self.add_attribute(name)
value = yield.freeze
self.define_singleton_method(name) do
value
end
end
end
Global.add_attribute(:topic_api_markdown_renderer){
@lulalala
lulalala / model.rb
Created December 11, 2014 02:45
Bug inside write_attribute
# insdie model
def write_attribute(attr_name, value)
super
# Association assignment take effect after super
case attr_name
when 'admin_editing'
pp "Inside write_attribute: #{self.admin_editing}"
end
end
@lulalala
lulalala / over_wrapping.rb
Created April 16, 2015 01:24
over wrapping
def foo(role)
if !teacher?(role)
...
def teacher?(role)
role == :teacher