ruby Methods, Procs, Lambdas, and Closures

作者: youxin
发布时间:2015-07-13 11:31:47


define simple method定义简单方法


#define a method  def    factorial(n)      if n<1          raise "argument must be >0"      elsif n==1          1      else           n*factorial(n-1)      end  end  puts factorial(5)






def fact2(n)      raise "bad argument " if n<1      return 1 if n==1      n*fact2(n-1)  end    puts fact2(5)


def polar(x,y)
reutrn Maht.hypot(y,x),Math.atan2(y,x)


在对象上调用方法 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语句为一个特定的对象定义方法,只需简单地在def关键字后加上一个求值结果为对象的表达式,在这个表达式 之后需要一个句点符号和要定义的方法名。这样定义的方法称为单键方法,因为他只在单个对象上可用:

o="message"  def o.printme      puts self  end  o.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

def sum(x,y)      x+y  end  puts 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




标签: Lambda Methods Ruby