Archive for Ruby

[DRB]Distributed Ruby Objects Simple Sample

Distributed Ruby Objects (以下簡稱 drb),是 Ruby Standard Library 中用來做到類似 RPC (Remote procedure call) 的函式庫,以下紀錄最基本的 Server 端和 Client 端的 sample。

Server:

require ‘drb’

class TimeServer

def get_current_time
Time.now
end

def stop_server
DRb.stop_service
end

end

uri = ‘druby://localhost:7788′
front_object = TimeServer.new

DRb.start_service(uri, front_object)

# Wait for the drb server thread to finish before exiting.
DRb.thread.join

這樣一來最簡單的 Server 端就OK了,只要執行這段 Ruby code,Client端就可以用 druby://localhost:7788 來叫用服務。

Client:

require ‘drb’

server_uri = “druby://localhost:7788″

timeserver = DRbObject.new_with_uri(server_uri)

#call remote object method ‘get_current_time’
puts timeserver.get_current_time

#stop remote service
timeserver.stop_server

就這麼簡單,不過方便歸方便,安全的問題也要特別注意

若想深入了解的人可以參考以下連結:

Intro to DRb
drb: Ruby Standard Library Documentation

張貼留言

如何在寫一般 Ruby Code 時使用 Rails 的 ActiveSupport

就只要改一行:

require “rubygems”
require “active_support”

就這麼簡單,害我找半天,本以為要像使用 ActiveRecord 般

require “rubygems”
require_gem “activerecord”

依樣畫葫蘆的結果完全沒反應

require “rubygems”
require_gem “activesupport”

怎麼會這樣勒?????
在Google中也沒找到相關資料,我記得require_gem “activerecord” 之後除了ActiveRecord外還可直接使用ActiveSupport,但若只想使用ActiveSupport的功能把整個ActiveRecord都require進來實在太肥,索性直接進 ActiveRecord 的原始碼裡面看到底是怎麼弄的,結果改一行就解決~~

YA~~ 以後可以直接使用 1.days.ago 或是 Time.now.tomorrow 這類簡單的表示法啦~~~

Rails萬歲~~~ OpenSource 萬歲~~~~~

留言 (2)

[Ruby] dump excel data example.

require 'win32ole'

def getAbsolutePath filename
  fso = WIN32OLE.new('Scripting.FileSystemObject')
  return fso.GetAbsolutePathName(filename)
end

filename = getAbsolutePath("sample1.xls")
xl = WIN32OLE.new('Excel.Application')
book = xl.Workbooks.Open(filename)

begin
book.Worksheets.each do |sheet|
  sheet.UsedRange.Rows.each do |row|
    record = []
    row.Columns.each do |cell|
      record << cell.Value
    end
    puts record.join(",")
  end
end

ensure 
  book.Close
  xl.Quit
end

Reference: Win32OLE 活用法 【第 2 回】 Excel

留言 (2)

如何在寫一般 Ruby Code 時使用 Rails 的 ActiveRecord 存取 Database

require "rubygems"
require_gem "activerecord"
require "active_record"

ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "hostname",
:database => "yourdb",
:username => "dbuser",
:password => "dbuserpw"
)

class MyTable < ActiveRecord::Base
end

這樣就可以在一般 Ruby Code 中使用 ActiveRecord 的 CRUD (Create, Read, Update, Delete) 存取你的 Database 了.

Update: 2007-03-16

因為 require_gem 已經被廢除了,所以改用 require ‘active_record’ 代替

另外,從 Lighty RoR 最簡潔有力的網頁框架: 在 Rails 之外使用 ActiveRecord 這篇中發現,還可以利用設定 config 的方式將 ActiveRecord 的設定寫在 code 外

想知道詳細用法可至上述網頁看看 :)

張貼留言

Simple ruby send mail example.

require ‘net/smtp’
msg = <<END_OF_MESSAGE

From: from_alias <from_where>
To: to_alias <to_where>
Subject: Ruby Mail Test

As title. This is a test mail sending by ruby.

END_OF_MESSAGE

Net::SMTP.start() do |smtp|
smtp.sendmail( msg, “from_where”, “to_where” )
end

That’s all.:D

Reference Link:
sendmail (Net::SMTP)

留言數(1)