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

2008年6月15日 星期日

中文字比較

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

2008年6月11日 星期三

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月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

2008年5月26日 星期一

symbol

change string to symbol:
use to_sym or intern
ex:
"test".to_sym
--->  :test

change symbol to string
method 1: use to_s
method 2: use #{ }
ex:
"#{:abc}"
---> "abc"

2008年5月25日 星期日

methods

methods:
A.methods:
show class methods
A.methods(false):
only show class methods of A, not including inherited methods
a.methods:
show instance methods of a

instance_methods:
A.instance_methods equals a.methods
A.instance_methods(false):
show only instance_methods 0f A, not including inherited methods

private_methods, public_methods, protected_methods, singleton_methods



compare: equal?, Comparable module

check  whether  two objects are the same object

define how to compare
use Comparable module & define <=> method
the return value of method <=> ,  1, 0 , & -1 decides greater than, equal than, & less than
ex:
class Rabbit
     include Comparable 
     attr_accessor  :name

     def  <=> (other_rabbit)
          if  self.name <>
                  -1
          elsif  self.name > other_rabbit.name
                  1
          else
                  0
          end 
     end
end

if the thing to compare know how to compare, there is a shorter definition
ex:
def  <=> (other_rabbit)
       self.name<=> other_rabbit.name
end


true, false & nil

In Ruby, nil & false  are the only two objects have boolean value of false

2008年5月24日 星期六

for, each

for
ex:
for a in [1,2,3]
puts a
end

each
ex:
[1,2,3].each do |i|
puts i
end

loop, while, until

loop
ex:
loop do
    a=a+1
    break if a>3
end

loop do 
     a=a+1
     next if a <3
     break
end

while
ex:
while a<3
end

until:
ex:
until a>3
end



case

ex:
case  name
when "peter"
puts "peter"
when "andy"
puts  "andy"
else
puts  "others"
end

how it works
translate to 
if "peter" === name

note: the === method can be customarily defined

private & protected methods

private method:
(1) use private  with argument
ex:
private  :test,  :run

(2) use private
ex:
private
def  test
end

def run 
end
the methods after private are private

protected method:
use protected

can call the object's protected method as long as self is an instance of the same class  as the object whose protected method is called


global variable

start with $

built-in global variables:
$: the directories that make up the path Ruby searches for external file

$0: name of the file Ruby is executing

$$: process id of Ruby process
ps:  use English.rb to replace these built-in global variables with meaningful names

2008年5月22日 星期四

instance variable

In the class definition, the instance variable insides method & outside method are not the same

the instance variable inside method belongs to object
the instance variable outside method belongs to class

class & self

In the class definition, self = class
so Rabbit.run = self.run

In the class-method definition,  self = class

:: access

(1) access constant

(2) access nested class
ex:
module ActionController
       module  Routing
              class Route
....
end

ActionController::Routing::Route.new

ex:
class  Rabbit <>
end

class ApplicationController <>
end

(3) access nested module

method_missing

If the message sent to object is the message object can't understand, it will trigger execution of method_missing

Rails use method_missing to look up database & create methods 

2008年5月20日 星期二

super keyword

call the method of superclass or module

ex:
class A
    def test(num)
           puts "A test #{num}"
    end
end

class B  <>
     def test( num)
           super
   puts "B test #{num}"
     end
end
b= B.new
b.test(2)
--->  A test 2
         B  test 2

super will pass the parameters to superclass or module

another usage:  call super will parameters to pass specific parameters


root module & root class

root class:  Object
root module:  Kernel

attr_reader, attr_writer,

the getter:
ex:
attr_reader  :name

the setter:
ex:
attr_writer :name

getter & setter:
ex:
attr_accessor :name