Programmering i Ruby

Den Pragmatiske Programmerers Veiledning

Forrige < Innhold ^
Neste >
klassen Method
Forelder: Object
Versjon: 1.6

Indeks:

[ ] arity call to_proc


Method objects are created by Object#method , and are associated with a particular object (not just with a class). They may be used to invoke the method within the object, and as a block associated with an iterator.

class Thing
  def square(n)
    n*n
  end
end
aThing  = Thing.new
aMethod = aThing.method("square")
aMethod.call(9) 81
[ 1, 2, 3 ].collect(&aMethod) [1, 4, 9]

instansmetoder
[ ] meth[ [ args ]* ] -> anObject

Synonym for Method.call.

arity meth.arity -> aFixnum

Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. For methods written in C, returns -1 if the call takes a variable number of arguments.

call meth.call( [ args ]* ) -> anObject

Invokes the meth with the specified arguments, returning the method's return value.

m = 12.method("+")
m.call(3) 15
m.call(20) 32

to_proc meth.to_proc -> aProc

Returns a Proc object corresponding to this method.


Forrige < Innhold ^
Neste >

Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide".
Translation to norwegian by Norway Ruby User Group.
Copyright for the english original authored by David Thomas and Andrew Hunt:
Copyright © 2001 Addison Wesley Longman, Inc.
This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).

(Please note that the license for the original has changed from the above. The above is the license of the original version that was used as a foundation for the translation efforts.)

Copyright for the norwegian translation:
Copyright © 2002 Norway Ruby User Group.
This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.