標籤:style blog http color 使用 os
define simple method定義簡單方法
關鍵字def用於方法定義,在其後是方法名和可選的參數名列表,參數名列表會用一對圓括弧括住。構成方法主體的代碼放在參數列表之後,end用於結束方法定義。
#define a methoddef factorial(n) if n<1 raise "argument must be >0" elsif n==1 1 else n*factorial(n-1) endendputs factorial(5)
方法傳回值
方法可能正常結束,也可能非正常結束。當拋出異常屬於非正常結束。
如果方法正常結束,方法代碼的最後一個運算式的值會作為方法調用運算式的值。
return關鍵字將在到達方法最後一個運算式前強行返回。如果return後面沒運算式,就返回ni。
def fact2(n) raise "bad argument " if n<1 return 1 if n==1 n*fact2(n-1)endputs fact2(5)
對方法的最後一行也可以使用return關鍵字,這可以強調這個運算式用作這個方法的傳回值。不過在一般的編程實踐中,如非必須,會省略return。
ruby方法可以返回多個值,這必須要顯式使用return語句,並把要返回的值用逗號分開:
def polar(x,y)
reutrn Maht.hypot(y,x),Math.atan2(y,x)
end
在對象上調用方法 invoking a method on a object
Methods are always invoked on an object. (This object is sometimes called the
receiver in a reference to an object-oriented paradigm in which methods are called
“messages” and are “sent to” receiver objects.) Within the body of a method, the keyword
self refers to the object on which the method was invoked. If we don’t specify
an object when invoking a method, then the method is implicitly invoked on self.
You’ll learn how to define methods for classes of objects in Chapter 7. Notice, however,
that you’ve already seen examples of invoking methods on objects, in code like this:
first = text.index(pattern)
Like most object-oriented languages, Ruby uses . to separate the object from the method
to be invoked on it. This code passes the value of the variable pattern to the method
named index of the object stored in the variable text, and stores the return value in the
variable first.
定義單鍵方法define a singleton method
目前為止,我們定義的方法都是全域方法。如果把前面的那些def語句放在一個class語句中,這些方法就成為了該類的執行個體方法,他們被定義在這個類的所有執行個體對象上。
不過,也可以使用def語句為一個特定的對象定義方法,只需簡單地在def關鍵字後加上一個求值結果為對象的運算式,在這個運算式 之後需要一個句點符號和要定義的方法名。這樣定義的方法稱為單鍵方法,因為他只在單個對象上可用:
o="message"def o.printme puts selfendo.printme #輸出message
如果在fixnum定義單鍵方法,會報錯:<main>‘: can‘t define singleton method "printme" for Fixnum (TypeError)
取消方法定義undef method
Methods are defined with the def statement and may be undefined with the undef
statement:
def sum(x,y) x+yendputs sum(1,2)undef sum
In this code, the def statement defines a global method, and undef undefines it. undef
also works within classes (which are the subject of Chapter 7) to undefine the instance
methods of the class. Interestingly, undef can be used to undefine inherited methods,
without affecting the definition of the method in the class from which it is inherited.
Suppose class A defines a method m, and class B is a subclass of A and therefore inherits
m. (Subclasses and inheritance are also explained in Chapter 7.) If you don’t want to
allow instances of class B to be able to invoke m, you can use undef m within the body
of the subclass.
undef is not a commonly used statement. In practice, it is much more common to
redefine a method with a new def statement than it is to undefine or delete the method.
Note that the undef statement must be followed by a single identifier that specifies the
method name. It cannot be used to undefine a singleton method in the way that def
can be used to define such a method.
Within a class or module, you can also use undef_method (a private method of Module)
to undefine methods. Pass a symbol representing the name of the method to be
undefined.