解決過的問題備忘錄 (外部記憶體?)

顯示具有 ruby 標籤的文章。 顯示所有文章
顯示具有 ruby 標籤的文章。 顯示所有文章

2010年8月7日 星期六

kates's ruby plurk lib 'add_all_as_friends' method doesn't work?

正體中文版在此 (Traditional Chinese version is here)

kates's ruby plurk lib is a nice plurk lib for ruby. But I notice that the 'add_all_as_friends' method doesn't work and always throws an exception (API call return an error message page and JSON parse failed)


Add following code before you new a Plurk::Client
module Plurk
  class Client
    ENDPOINTS[:add_all_as_friends] = {:path=>"/API/Alerts/addAllAsFriends", :login_required=>true}
  end
end

This is because that the API path is /API/Alerts/addAllAsFriends not /API/Alerts/addAllAsFriend. The code shows that they amiss lost the ending 's'. And I will contact them to fix this.

2010年7月11日 星期日

Ruby Multi-threading simple tutorial

本部落格已搬家,轉址中... Ruby Multi-threading simple tutorial

2010年6月20日 星期日

Gets undefined method error when calling parse method?

irb(main):001:0>  Time.parse("16:30")
NoMethodError: undefined method `parse' for Time:Class

Also rfc2822(), httpdate() and xmlschema(date) raises this error.


Solution: require the time.rb:
require 'time'

This is not a bug. It's because they are not core (But Time class is). Some implementation may auto require for you. But if you get this error, just try to require it.

Reference 1, 2



==Traditional Chinese version==

呼叫Time.parse時得到undefined method錯誤?

irb(main):001:0>  Time.parse("16:30")
NoMethodError: undefined method `parse' for Time:Class

rfc2822(), httpdate() and xmlschema(date) 也會爆這個錯誤


解決方法: require time.rb:
require 'time'

這不是bug,這是因為這些methods不是core(但Time class本身是),有些implementation可能會自動幫你require,但如果你遇到這個error,就試試看require 'time'

參考來源 1, 2

2010年6月12日 星期六

(Ruby) Write service with daemons gem

How to make a ruby script run only one instance at a time? and with start/stop command?

Actually, it's very simple, just use Daemons gem.

installation (ubuntu):
sudo gem install daemons

Simplest example: test.rb
require 'rubygems'
require 'daemons'

Daemons.run_proc('myproc.rb') do
  loop do
    sleep(5)
    puts "Hello World"
  end
end

Than you can use following commands:
ruby test.rb [start|stop|restart|run|zap]

(Run 'ruby test.rb' will get command usage.)

And when you try to run 'test.rb start' twice. you will get:
ERROR: there is already one or more instance(s) of the program running



==Traditional Chinese version==


如何讓一支ruby程式一次只有一個instance?並且有start/stop等指令呢?

其實,這非常簡單,只要用Daemons gem就可以了。

安裝 (ubuntu):
sudo gem install daemons

最簡單的範例: test.rb
require 'rubygems'
require 'daemons'

Daemons.run_proc('myproc.rb') do
  loop do
    sleep(5)
    puts "Hello World"
  end
end

然後你就可以用以下指令了:
ruby test.rb [start|stop|restart|run|zap]

(執行 'ruby test.rb' 會提示你指令用法)

當你嘗試執行 'test.rb start' 兩次,你會得到以下錯誤:
ERROR: there is already one or more instance(s) of the program running