switch_user 0.9 released

    31 Oct 2012

    switch_user provides a convenient way to switch current user that speeds up your development and reproduce user specified error on production.

    Today switch_user gem 0.9.0 is released, all thanks to Luke Cowell.

    He is a collaborator of switch_user gem, and did a great job for 0.9.0 gem.

    1. did lots of refactors.
    2. added unit tests.
    3. made switch_user a rails engine.

    check out the changelog here.

    I also updated switch_user example to use switch_user 0.9.0.

    Read More


    how to write a jruby gem - part 2

    23 Aug 2012

    In my previous post, I introduced how to write a jruby gem with ruby code, today I will show you how to write a jruby extension with java code, which can give you better performance.

    Standard Steps

    1. create java classes to wrap any java library you need, and the java classes must extend RubyObject, then it can be called from jruby. e.g.

    class Memcached extends RubyObject {
        // MemcachedClient is what we want to wrap
        private MemcachedClient client;
        // java constructor
        public Memcached...
    Read More


    how to write a jruby gem - part 1

    06 Aug 2012

    In my previous post, I mentioned I have written a jruby memcached gem. I'm glad to share my experience how to extend jruby here.

    JRuby is a 100% java implementation of ruby programming language, it allows you calling java code from ruby code. Java world has much more libraries than ruby gems, to make use of those java jar, it makes your code easier and faster.

    I assume you already had the exper...

    Read More


    css sprite best practices

    03 Apr 2010

    css sprite最佳实践(中文版)

    (Updated on 2010-4-4, thank Scott Ballantyne )

    The advantage of using the css sprite is to reduce a large number of http requests, so it makes the web page loaded much faster. I often find it it painful for me to compose a lot of images into one css sprite image and measure the x and y positions for each image.

    Last year, I wrote a css_sprite gem, but to use it you need to define all the images you want to do the css sprite in the configuration file, and it is not easy to use. Because of this, recently I rewrote the css_sprite gem, it is not necessary to use configuration file any more by default, th...

    Read More


    css sprite最佳实践

    02 Apr 2010

    css sprite best practices (english version)

    应用css sprite的好处在于可以大量减少http请求数,从而达到更快加载页面的效果。


    去年我就写了一个css_sprite的插件,但是由于需要在配置文件中定义所有需要组合在一起的图片,用起来还是很麻烦,不够傻瓜化。最近我把css_sprite插件重写了一遍,默认不需要使用配置文件,遵循rails的Convention Over Configuration的思想,可以做到全自动的css sprite操作。


    上图中蓝色部分就是Convention的css sprite目录,也就是在public/images目录下面的css_sprite目录或者以css_sprite结尾的目录(比如another_css_sprite),需要执行css_sprite操作。

    绿色部分则是需要被css sprite的图片,你可以动态的在css sprite目录下面增加或删除图片,css sprite操作...

    Read More


    oauth for twitter

    02 Dec 2009

    twitter是Twitter的ruby gem,它提供了两种身份验证的方法,一是oauth,二是http auth。http auth非常简单,只要提供账号和密码就可以了,而oauth就稍微复杂一些了。

    首先,你需要到去注册你的应用,并得到相应的consumer token和consumer secret。

    接着就可以使用twitter gem了

    def oauth
      oauth =, consumer_secret)
      request_token = oauth.set_callback_url ''
      session[:rtoken] = request_token.token
      session[:rsecret] = request_token.secret
      redirect_to request_token.authorize_url


    Read More



    25 Oct 2009


    irb(main):001:0> require 'rubygems'
    => true
    irb(main):002:0> require 'activerecord'
    => true
    irb(main):003:0> ActiveRecord::VERSION::STRING
    => "2.3.4"


    irb(main):001:0> require 'rubygems'
    => true
    irb(main):002:0> gem 'activerecord', '2.3.2'
    => true
    irb(main):003:0> require 'activerecord'
    => true
    irb(main):004:0> ActiveRecord::VERSION::STRING
    => "2.3.2"


    Read More


    add gem rake for rails

    30 Sep 2009

    使用过rails插件的一定知道,只要在插件的tasks目录下面定义rake文件,rails就会自动加入其中定义的task。但是gem就不能这样用了,即使rake gems:unpack也没用。

    解决的方法是将定义在gem中的task require到rails目录下。比如我在css_sprite gem的lib/css_sprite.rb中定义

    unless Rake::Task.task_defined? "css_sprite:build"
      load File.join(File.dirname(__FILE__), '..', 'tasks', 'css_sprite_tasks.rake')

    上面3行的意思是如果当前的tasks中没有css_sprite:build的话,就load gem中的tasks/css_sprite_tasks.rake。

    然后在rails app中增加lib/tasks/css_sprite.rake

    require 'css_sprite'

    这样你就可以使用rake css_sprite:build task了。

    更懒的方法是直接把require css_sprite'加到rails目录下的Rakefile。...

    Read More


    add executable to ruby gem

    26 Sep 2009



    require 'rfetion'
    require 'rfetion/command'


    require 'optparse'
    options = {} do |opts|
      # Set a banner, displayed at the top of the help screen.
      opts.banner = "Usage: rfetion [options]"
      opts.on('-m', '--mobile MOBILE', 'Fetion mobile number') do |mobile|
        options[:mobile_no] = mobile
      opts.on('-p', '--password PASSWORD', 'Fetion...
    Read More



    02 Aug 2009





    1. 安装jeweler gem。
    $ gem install jeweler
    1. 在Rakefile文件中增加新的task
    require 'jeweler' do |gemspec| = "regexp_crawler"
      gemspec.summary = "RegexpCrawler is a Ruby library for crawl data from website using regular expression."
      gemspec.description = "RegexpCrawler is a Ruby library for crawl data from website using reg...
    Read More