2008年6月15日 星期日

中文字比較

一個中文字利用3byte表示,
因此若要比較a="我愛妳"和b="妳愛我"的字首,
要比較  a[0]==b[0] && a[1]==b[1] && a[2] == b[2]

2008年6月11日 星期三

rails API

api.rubyonrails.org

included & extend

included:
the method defines the action to do when the class or module get included 

extend:
extend the class's ability by including module
ex:
module M
end
Class C
end
c= C.new
c.extend(M)

note:  use object to extend, get instance method, 
           use class to extend, get class method

class_eval

define method for class
ex:
A=Class.new
A.class_eval do
      def  test
          puts "test"
      end
end
a=A.new
a.test
--->  test

2008年6月10日 星期二

partial

include the view in other file

ex:
render :partial=> "books"
render the view in _books.html.erb (  the partial view file must 
start with _ )

ex:
render :partial => "rabbit/list"
render the view in different directory

note: the variable partial view can access is the variable the master view can access 

2008年6月8日 星期日

self & method

when there is a method ending with =,  such as test=, 
and there is another method  run
In the definition of run, there are two conditions:
(1)  test=3
      this means test is a local variable
(2) self.test=2
      this will call test method with parameter 2

Hence, when calling the method ending with = in the method definition, we can not omit self

association

use class methods( has_one, belongs_to, has_many,  has_and_belongs_to_many)
to create instance methods

has_one & belongs_to

has_many & belongs_to

has_and_belongs_to_many

the parameter for these methods:
:dependent => true
for delete.  
ex:
class Customer <>
         has_many  :orders,
                             :dependent=>true
end
if the customer A is deleted, its orders are also deleted

:order
ex:
:order=>"created_at ASC"
ascending order according to created_at field