最近看见两张图,很有意思,废话少说,直接上图:
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!


最新评论