rubygems

    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

    Tags 


    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

    Tags 


    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

    Tags 


    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

    Tags 


    css sprite最佳实践

    02 Apr 2010

    css sprite best practices (english version)

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

    但是对于像我这样的懒人,你让我每次都一个一个把图片copy到一个css_sprite图片里,还得量一下这个每个图片对应的x和y坐标,实在是一种折磨。

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

    首先,让我们看看目录结构的Convention是如何定义的

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

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

    Read More

    Tags 


    oauth for twitter

    02 Dec 2009

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

    首先,你需要到http://twitter.com/oauth_clients去注册你的应用,并得到相应的consumer token和consumer secret。

    接着就可以使用twitter gem了

    def oauth
      oauth = Twitter::OAuth.new(consumer_token, consumer_secret)
      request_token = oauth.set_callback_url 'http://www.huangzhimin.com/'
      session[:rtoken] = request_token.token
      session[:rsecret] = request_token.secret
    
      redirect_to request_token.authorize_url
    end

    注意,这里除了传入consumer_token和consumer_secret之外,还设置了callback_url,它表示twitter身份验...

    Read More

    Tags 


    require某个版本的gem

    25 Oct 2009

    一般我们require一个gem,都是require最新的版本

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

    如果需要require一个旧版本的gem该怎么办呢?只需要在require之前指定gem的版本即可

    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"

    是不是看上去和rails的用法蛮像的?

    Read More

    Tags 


    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')
    end

    上面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

    Tags 


    add executable to ruby gem

    26 Sep 2009

    刚刚给rfetion增加了executable,使得用户可以直接在shell下面发送短信。

    首先,在gem目录下增加bin目录,并增加executable文件

    require 'rfetion'
    require 'rfetion/command'

    接着新增command.rb文件来处理用户输入,用的是ruby类库自带的optparse

    require 'optparse'
    
    options = {}
    
    OptionParser.new 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
      end
    
      opts.on('-p', '--password PASSWORD', 'Fetion...
    Read More

    Tags 


    在github上发布gem

    02 Aug 2009

    现在ruby上流行的gem一般都在两个repository上,一个是rubyforge,一个是github。

    这几天在写regexp_crawler,通过正则表达式来爬取网上的数据。今天写得差不多了,就想在github上以gem的形式发布。

    首先,在自己项目的编辑页面,把RubyGem这项勾上。

    接着,就是生成gemspec文件。我是通过jeweler来管理自己gemspec的。

    1. 安装jeweler gem。
    $ gem install jeweler
    1. 在Rakefile文件中增加新的task
    require 'jeweler'
    
    Jeweler::Tasks.new do |gemspec|
      gemspec.name = "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

    Tags