ruby - What is the difference between a method and a proc object? -
i know methods in ruby not objects procs , lambdas are. there difference between them other that? because both can pass around. makes proc objects different method?
method:
1.8.7-p334 :017 > def a_method(a,b) 1.8.7-p334 :018?> puts "a method args: #{a}, #{b}" 1.8.7-p334 :019?> end 1.8.7-p334 :021 > meth_ref = object.method("a_method") => #<method: class(object)#a_method> 1.8.7-p334 :022 > meth_ref.call(2,3)
proc object:
= lambda {|a, b| puts "#{a}, #{b}"} a.call(2,3)
you said in question "methods not objects" have careful distinguish between "method" , "method".
a "method" defined set of expressions given name , put method table of particular class easy lookup , execution later.
a "method" object (or "unboundmethod" object) actual ruby object created calling method
/ instance_method
/ etc. , passing name of "method" argument.
you may find useful read rdoc documentation unboundmethod, method, , proc. rdoc pages list different instance methods available each type.
basically, method
object "bound" object self
points object when call
method, , proc
doesn't have behavior; self
depends on context in proc
created/called.
Comments
Post a Comment