Skip to content

Instantly share code, notes, and snippets.

@3dd13
3dd13 / ruby_core_source.rb
Created August 31, 2011 08:01
the reason of ruby_core_source.rb:57 'initilize': not in gzip format (Zlib::GzipFile::Error)
#
# the problem was
# this file was TAR compressed but not TAR.GZ
#
uri_path = "http://ftp.ruby-lang.org/pub/ruby/1.9/" + ruby_dir + ".tar.gz"
Tempfile.open("ruby-src") { |temp|
temp.binmode
@3dd13
3dd13 / workless_delayed_job_initializer_mongoid.rb
Created March 13, 2011 15:00
use workless with Delayed Job Mongoid https://github.com/collectiveidea/delayed_job_mongoid https://github.com/lostboy/workless To use: after setting up delayed_job_mongoid and workless, put this file under initialzers directory
Delayed::Worker.max_attempts = 3
Delayed::Backend::Mongoid::Job.send(:include, Delayed::Workless::Scaler) if defined?(Delayed::Backend::Mongoid::Job)
@3dd13
3dd13 / application.html.erb
Created February 27, 2011 15:21
Sample string interpolation in en.yml with ruby I18n.t method
<html>
...
<body>
...
<div id="footer">
<%= I18n.t("layout.copyright_statement", :current_year => Date.today.year) %>
</div>
...
</body>
</html>
@3dd13
3dd13 / row_is_cut_in_the_middle_solution.rb
Created February 18, 2011 07:49
overflow / page break examples of Prawn Pdf generation.
# gem "prawn", "0.8.4"
# Sometime there is no enough space for the last table row when it reaches the end of page
Prawn::Document.generate("text_group_overflow_question.pdf") do |pdf|
add_page_break_if_overflow(pdf) do |pdf|
# generating table here
# ...
end
end
@3dd13
3dd13 / clear_log.rb
Created January 27, 2011 06:34
Adding log purging job using chef
cron "housekeep application log files" do
user "housekeeper"
# mailto "your@example.com"
# 00:01 every night
hour "0"
minute "1"
# clean up logs older than 30 days
command "/find ~/log/application-*.log.tar.gz -mtime +30 -exec rm {} \;"
@3dd13
3dd13 / log_rotation.rb
Created January 21, 2011 02:46
Log Rotation methods provided by Ruby Core http://ruby-doc.org/ruby-1.9/classes/Logger.html
def logger
@logger ||= Logger.new(File.new("data_loader.log"),3,5*1024*1024)
end
@3dd13
3dd13 / stock_daily_price.rb
Created January 21, 2011 02:39
Download stock price data from yahoo and insert them into database
require 'rubygems'
require 'active_record'
require 'yaml'
dbconfig = YAML::load(File.new(File.join(File.dirname(__FILE__), "../../config/database.yml")))
ActiveRecord::Base.establish_connection(dbconfig["development"])
class StockDailyPrice < ActiveRecord::Base
def initialize(stock_values, stock_no = "")
super()
@3dd13
3dd13 / generate_ssh_keys.rb
Created January 20, 2011 16:59
chef recipe to generate ssh key for a user
define :generate_ssh_keys, :user_account => nil do
username = params[:user_account]
raise ":user_account should be provided." if username.nil?
Chef::Log.debug("generate ssh skys for #{username}.")
execute "generate ssh skys for #{username}." do
user username
creates "/home/#{username}/.ssh/id_rsa.pub"
@3dd13
3dd13 / 1_controller_test.rb
Created January 7, 2011 07:04
sharing on functional test refactoring with code block. cleaner and more readable.
test "not allow to update state after account reconciliation" do
# get fixture
account = accounts(:fresh)
# submit and expect no errors
post :waive_fee, :id => account.id
assert_nil flash[:error]
post :add_fee, :id => account.id
assert_nil flash[:error]
@3dd13
3dd13 / css_xpath_selector_benchmarking.rb
Created November 5, 2010 04:00
just curious about CSS and XPath selector performance. did some benchmarking
require 'rubygems'
require 'mechanize'
require 'benchmark'
agent = Mechanize.new
# however, this fetch already spend around 2 seconds. one http request response.
page = agent.get('http://www.ufood.com.hk/search/search.action?name=&distIds=4,10,11,3,5,8,6,7,2,1,9,12,13,14,15,18,16,17,19,20,21,22,23,24,25,26,33,34,27,35,32,30,29,28,31&ftIds=')
Benchmark.bm do |b|
# search by tag name and class 10.470000 0.040000 10.510000 ( 11.149709)