最近看见两张图,很有意思,废话少说,直接上图:

关于编程语言
language fighting

关于操作系统
OS fighting

haha, I am playing with Ruby on Linux!

  1. 最近读了一篇文章,里面介绍了Session和Cookie在RoR开发中的使用方法,英文还不错的同学请直接读这篇文章。我仅仅在这篇Post里面做个整理,把那些有用的资料在此备份,免得下回又的麻烦Google。上面提到的文章很全面的介绍了很多细节,目录如下:
    1. Introduction
    2. Sessions
      1. Session in rails
      2. Configure your sessions
      3. Storage options
      4. Session storage limitations
      5. Session and Security
      6. HowTo
        1. Implement session expiration
        2. Delete stale sessions
        3. Find out active users
        4. Access session data using session_id
      7. Miscellaneous
    3. Cookies
      1. Cookie on rails
      2. cookies vs. request.cookies
      3. CookieJar
      4. Miscellaneous

In weekly code review, GL mentioned a weird issues :

1
2
3
4
5
6
7
8
9
10
def self.test(obj)
    case obj.class
    when Student, TrialPlan
      puts "1"
    when Call
      puts "2"
    else
      puts "3"
    end
end

This doesn’t behave as expected. When we run test(Student.first), it prints out “3″!

but this works fine.

1
2
3
4
5
6
7
8
9
10
def self.test(obj)
    case obj.class.to_s
    when "Student", "TrialPlan"
      puts "1"
    when "Call"
      puts "2"
    else
      puts "3"
    end
end

Then I searched around for explaining this, I found the case statement in Ruby is very different with what in other languages. Ruby implements the Case statement according to its special operator “===”, which is not the same as “==”.

what’s difference? See below explainations:
how-a-ruby-case-statement-works-and-what-you-can-do-with-it/
ruby-case-statement-comparison-feature.html

Rails Best Practice Snippet: Model refactor.
Reference link to the PDF.Rails-best-practices.

Before:

1
2
3
4
5
6
class Invoice < ActiveRecord::Base
  belongs_to :user
end
<%= @invoice.user.name %>
<%= @invoice.user.address %>
<%= @invoice.user.cellphone %>

After:

1
2
3
4
5
6
7
class Invoice < ActiveRecord::Base
  belongs_to :user
  delegate :name, :address, :cellphone, :to => :user, :prefix => true
end
<%= @invoice.user_name %>
<%= @invoice.user_address %>
<%= @invoice.user_cellphone %>

See more details about Delegate in rails doc.

目前最常用的方法是利用Unix自身的cron功能来实现定时任务,但是cron的语法比较麻烦(个人感觉),今天为了执行一个定时任务发现了一个很好的RubyGem可以让你用Ruby的方式定义定时任务,这就是Whenever

Whenever可以让你写出类似下面的定时任务:

 set :cron_log, "/path/to/my/cron_log.log"

 every 2.hours do
   command "/usr/bin/some_great_command"
   runner "MyModel.some_method"
   rake "some:great:rake:task"
 end

赶快去Github看看文档和代码吧http://github.com/javan/whenever

There are a number of ways of parsing an RSS feed in Ruby, but one of the best is a gem called Feedzirra. The main advantage of Feedzirra is its speed; it parses feeds very quickly, but it is also useful as it can parse many different types of feed.

To install Feedzirra we first need to make sure that

1
http://gems.github.com

is in our list of gem sources. If not we’ll need to add it.

gem sources -a http://gems.github.com

Now we can install the gem:

sudo gem install pauldix-feedzirra

Notice: When I was installing the gem, there are several libs needed, including libcurl4-gnutls-dev libcurl3-gnutls libxslt1-dev, you may not meet this issue, it’s not a big deal, just install what it rely on. Several dependencies will also be installed alongside the gem. Once everything’s installed we’ll need to add a reference to the gem in our application’s /config/environment.rb file.

config.gem "pauldix-feedzirra", :lib => "feedzirra", :source => "http://gems.github.com"

That’s it. We’re ready to start parsing RSS feeds in our application.

Getting start:
Assume that, you’ve got a model to store the feed content which is fetched by the parser, so the model should be like :

class FeedEntry < ActiveRecord::Base
  def self.update_from_feed(feed_url)
    feed = Feedzirra::Feed.fetch_and_parse(feed_url)
    add_entries(feed.entries)
  end

  private
  def self.add_entries(entries)
    entries.each do |entry|
      unless exists? :guid => entry.id
        create!(
          :name         => entry.title,
          :summary      => entry.summary,
          :url          => entry.url,
          :published_at => entry.published,
          :guid         => entry.id
        )
      end
    end
  end
end

A more detailed manual is here!

© 2011 Refactoring Thoughts Suffusion theme by Sayontan Sinha
普人特福的博客cnzz&51la for wordpress,cnzz for wordpress,51la for wordpress