From: Brian Candler Date: 2003-04-10T06:48:26+09:00 Subject: Re: Proc Question On Thu, Apr 10, 2003 at 05:59:04AM +0900, Chris Pine wrote: > The block parameter list is closer to parallel assignment than a method > call. e.g. the following are pretty much the same: > ---------------------------- > > I don't know what you mean by that. They are all three similar, as is this: > > irb(main):003:0> a = [3,4,5] > => [3, 4, 5] > irb(main):004:0> b = [1,2,*a] > => [1, 2, 3, 4, 5] > > Because they all use arrays (as I understand it). I thought what you were highlighting was the difference between the syntax of method calls and proc parameters. There are several differences in the way arity is handled, for example. I was just pointing out that if you compare proc parameters to parallel assignments, rather than method calls, they are identical. I too feel there could perhaps be a more unified underlying handling, and indeed I do get caught out occasionally by foo.each do |v| .... end where foo is a hash, and I end up getting [key,value] passed in when I would rather have an argument error. But pragmatically I see cases where this is also useful. > The beginning of a method binds the parameters to the arguments passed in (I > hope I'm getting my vocabulary right) in exactly the same way, again, as I > understand it. They don't work in exactly the same way. For example: def test(a) puts a.inspect end test(1,2) #>> ArgumentError: wrong # of arguments(2 for 1) test = proc{|a| puts a.inspect} test.call(1,2) #>> [1, 2] > ---------------------------- > Ruby doesn't have "functions" - it does have Methods and Procs. > ---------------------------- > > What do you think a proc is? You can even create one with `lambda'! We all > agree that they are closures, right? Closures are functions (with certain > properties, of course) by any definition I have ever seen. I think perhaps we are just coming from different backgrounds - yours mathematics/computer science, mine programming (and low-level at that) In C, a function is a named procedure which takes arguments, performs some operations, and (optionally) returns a value. In Ruby, that's a method. To you, the word 'function' may mean something else. Ruby doesn't in itself have anything called a 'function' that I am aware of, so if you choose to apply that label to a Proc then fine; but I choose to apply it to a Method :-) > A proc is just a > wrapped function, and you call it (the function) with the `call' method > (i.e. calling the proc's method). And then you can also argue that a method is just a wrapped proc; this is explicit if you say self.class.instance_eval { define_method(:foo, proc { |x| x*2 }) } rather than the more sugary def foo(x) x*2 end Once it has become a Method it gains additional properties though, as you point out below. > Methods, on the other hand, seem quite different to me (even if they are > implemented with procs much like parameter lists are implemented with > arrays). It seems like, instead of being bound to any particular > environment, they are just bound "to the object". Plus, they are shared > between all objects of that class... that's a big difference. You would > need a different proc for each object to get the same effect. Sure. You have instance methods shared between all objects of that class, and you have singleton methods associated with a single object. I wasn't trying to say that Procs and Methods were the same, as they clearly aren't... sorry if it came across that way! Regards, Brian.