Procedure vs method

P

Phlip

Kless said:
What advantage or difference has a procedure (Proc) over a method?

Is this for homework or something?

Ruby has no procedures, only methods. All methods have a hidden 'self'
variable linking them to an object.

Beyond this, you are going to have to ask a more specific question -
preferrably one not answered by any tutorial - so we can give the best
answer!
 
D

David A. Black

Hi --

What advantage or difference has a procedure (Proc) over a method?

A method is executed as a direct result of sending a message to an
object:

"hello".upcase # the message is 'upcase', which resolves to a
# method name

A Proc object gets called by sending *it* a message:

pr = Proc.new { puts "I am a Proc!" }
pr.call # prints: I am a Proc!

Every method is defined inside a class or module, and executing the
method depends on having an object that will be able to find the
method. Procs, however, are handed around like other objects: a Proc
you create in one context can be called in another. Procs do not
belong to classes or modules.

There's more, but those are some basic differences.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
D

David A. Black

Hi --

Is this for homework or something?

Ruby has no procedures, only methods. All methods have a hidden 'self'
variable linking them to an object.

I think you missed the "(Proc)" in the question. The question is about
Proc objects (which I think it's reasonable to refer to as procedures,
though typically people don't) in comparison with methods.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
R

Robert Klemme

2008/10/12 David A. Black said:
I think you missed the "(Proc)" in the question. The question is about
Proc objects (which I think it's reasonable to refer to as procedures,
though typically people don't) in comparison with methods.

IMHO it makes sense to not refer to Procs as "procedures" because they
actually are _closures_. This is a significant difference.

Kind regards

robert
 
S

Sebastian Hungerecker

Kless said:
I wanted to know why or when is used Proc because it looks that is the
same that a method.

Often you use Procs that have been created from a block. Using a method
instead of a proc in that case is not really an option.
Other times you use Procs that you want to eventually pass to a method as
parameter. You could use methods there, but that would somewhat pollute the
namespace by defining a new method that you're never gonna call directly
anyway.

HTH,
Sebastian
 
M

Martin DeMello

IMHO it makes sense to not refer to Procs as "procedures" because they
actually are _closures_. This is a significant difference.

For the original poster, here's a quick illustration:

$ cat test.rb
a = 42

def foo
puts a
end

bar = Proc.new { puts a }

puts "calling bar"
bar.call

puts "calling foo"
foo

$ ruby test.rb
calling bar
42
calling foo
test.rb:4:in `foo': undefined local variable or method `a' for
main:Object (NameError)
from test.rb:13
 
D

David A. Black

Hi --

IMHO it makes sense to not refer to Procs as "procedures" because they
actually are _closures_. This is a significant difference.

Doesn't that vary by language though? I assume Proc/proc stands for
"procedure", so it's hard to rule that out as something to call them.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
R

Robert Klemme

2008/10/13 Phlip said:
An important goal of encapsulation is to give everything the narrowest scope
possible. Without closure, if we need 'a' to have a lifespan as long as
'bar', we have to make 'a' into a data member. That widens 'a's scope.

With closures, 'a' has the narrowest scope possible over a long lifespan.
That is a major win - and it's why platforms without closures don't know
what they are missing!

I agree that it's a powerful means but I'm not as enthusiastic as you
are. After all in OO languages you can easily emulate a closure by
creating a class with appropriate methods and data members. Closures
can on the other hand make code harder to comprehend because the
interaction between the closure and the local variable is not obvious.

Cheers

robert
 
R

Robert Klemme

2008/10/13 David A. Black said:
Doesn't that vary by language though? I assume Proc/proc stands for
"procedure", so it's hard to rule that out as something to call them.

Well, probably you are right although I do not know a language where
procedures are closures. OTOH you can say "proc" != "procedure".

Anyway, I just wanted to point out that a Proc is more than a simple
procedure that accepts parameters works on them and probably returns
something.

Kind regards

robert
 
D

David A. Black

Well, probably you are right although I do not know a language where
procedures are closures.

Aren't they in Lisp? I don't mean that the terms are interchangeable,
but are there procedures in Lisp that are not closures on the bindings
in effect when they're created? (I think there are in Scheme.)
OTOH you can say "proc" != "procedure".

What else would it mean though?
Anyway, I just wanted to point out that a Proc is more than a simple
procedure that accepts parameters works on them and probably returns
something.

That's the "There's more" part of my original post :) Closures, and
also method objects, which I didn't mention, were the two things I had
chiefly in mind.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,199
Messages
2,571,045
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top