[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Procedure vs method

Kless

10/12/2008 8:22:00 PM

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

Phlip

10/12/2008 9:19:00 PM

0

Kless wrote:

> 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!

--
Phlip
http://broadcast.oreilly.com/2008/10/testing-rails-par...


David A. Black

10/12/2008 9:24:00 PM

0

Hi --

On Mon, 13 Oct 2008, Kless wrote:

> 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.r... for details and updates!

David A. Black

10/12/2008 9:29:00 PM

0

Hi --

On Mon, 13 Oct 2008, Phlip wrote:

> Kless wrote:
>
>> 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.

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.r... for details and updates!

Robert Klemme

10/13/2008 8:03:00 AM

0

2008/10/12 David A. Black <dblack@rubypal.com>:
> On Mon, 13 Oct 2008, Phlip wrote:

>> 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.

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

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end

Kless

10/13/2008 8:39:00 AM

0

On 12 oct, 22:18, "Phlip" <phlip2...@gmail.com> wrote:
> Is this for homework or something?
I wanted to know why or when is used Proc because it looks that is the
same that a method.

Sebastian Hungerecker

10/13/2008 9:03:00 AM

0

Kless wrote:
> 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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Martin DeMello

10/13/2008 9:35:00 AM

0

On Mon, Oct 13, 2008 at 1:02 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> 2008/10/12 David A. Black <dblack@rubypal.com>:
>> On Mon, 13 Oct 2008, Phlip wrote:
>
>>> 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.
>
> 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

David A. Black

10/13/2008 10:24:00 AM

0

Hi --

On Mon, 13 Oct 2008, Robert Klemme wrote:

> 2008/10/12 David A. Black <dblack@rubypal.com>:
>> On Mon, 13 Oct 2008, Phlip wrote:
>
>>> 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.
>
> 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.r... for details and updates!

Phlip

10/13/2008 10:44:00 AM

0

Martin DeMello wrote:

>> 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:

For the Ruby authors, here's an upgrade:

> a = 42

> bar = Proc.new { puts a }

You can store 'bar' & use it later, and it stores its link to 'a'.

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!

--
Phlip



Robert Klemme

10/13/2008 11:28:00 AM

0

2008/10/13 Phlip <phlip2005@gmail.com>:

> 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

--
remember.guy do |as, often| as.you_can - without end