2009年3月4日星期三

Ruby ADO

require 'win32ole'
conn = WIN32OLE.new('ADODB.Connection')

strCnxn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\\My Documents\\db1.mdb;Persist Security Info=False"
conn.Open strCnxn

rsCustomers = WIN32OLE.new("ADODB.Recordset")
strSQLCustomers = "select * from test1"

rsCustomers =conn.execute(strSQLCustomers)
rsCustomers.MoveFirst()
print "test#{rsCustomers.Fields}"
puts rsCustomers.Fields.item('test').value # field value
puts rsCustomers.Fields.item('test').name #field name
puts rsCustomers.Fields.Count

2009年1月20日星期二

ruby module include , class send

in rails's codes there is a wondful use of ruby module,just see following codes:

module MyTools
def run(*args)
puts "let #{args.join(' ')} run"
end
end
class Klass
def hello(*args)

"Hello " + args.join(' ')
end
end
class TryTools
#include MyTools
def initialize()
self.class.send :include,MyTools
end
end
k = Klass.new
k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
c=TryTools.new
c.run("me")

Notes:

A couple of points about the include statement before we go on. First, it has nothing to do with files. C programmers use a preprocessor directive called #include to insert the contents of one file into another during compilation. The Ruby include statement simply makes a reference to a named module. If that module is in a separate file, you must use require to drag that file in before using include. Second, a Ruby include does not simply copy the module's instance methods into the class. Instead, it makes a reference from the class to the included module. If multiple classes include that module, they'll all point to the same thing. If you change the definition of a method within a module, even while your program is running, all classes that include that module will exhibit the new behavior.[Of course, we're speaking only of methods here. Instance variables are always per-object, for example.]

References : http://www.rubycentral.com/pickaxe/tut_modules.html

another words:



The extend method will mix a module’s methods at the class level. The method defined in the Math module can be used as a class/static method.

The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods.

http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/

2009年1月5日星期一

hpricot docs

recently I can not access the offical website of hpricot , https://code.whytheluckystiff.net/hpricot/ .
this is a RDoc reference here:
http://noobkit.com/show/ruby/gems/syntax/hpricot/hpricot/elements.html

Debugging Ruby and ROR

1,install rdebug, sudo gem install ruby-debug
2,command line for debugging ruby file , rdebug rubyscript.rb
3,tips for debugging rails app , insert 'debugger' statement into your code where the code you want to check
4,run ./script/server --debugger,then the app will trap into rdebug when execution flow encounters the 'debugger' statement.