[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: general question (different between methods and functions)?

SonOfLilit

3/28/2007 8:42:00 AM

Methods are associated with a receiver object.

Functions are not.

The only things you can call functions in Ruby are lambdas, blocks,
... Even then it's not clear whether they are methods or functions.

Everything else is methods.


AFAIK.


Aur

On 3/28/07, Yamal Khaled Soueidan <jkhaledsoueidan@gmail.com> wrote:
> Hello everyone,
>
> I have been discussing the difference between methods and functions with a
> friend, but we couldn't agree on what method and function is?
>
> I would be very glad if people can join us.
> At the moment we have 2 definition for the names.
>
> 1. first, both names are the same, you can use whatever you want, they refer
> to the same thing.
>
> 2. functions is used outside class, so you can call them direct, they are
> global functions, whereas methods are inside classes, and you cannot call
> them direct without creating object of that class which have the methods.
>
> What do you agree on, or what do you have in mind, when you hear function or
> method?
>
> and Thanks :)
>

5 Answers

James Gray

3/28/2007 11:57:00 AM

0

On Mar 28, 2007, at 3:42 AM, SonOfLilit wrote:

> Methods are associated with a receiver object.
>
> Functions are not.
>
> The only things you can call functions in Ruby are lambdas, blocks,
> ... Even then it's not clear whether they are methods or functions.
>
> Everything else is methods.

The above is all true, but we do sometimes refer to calling methods
in a "function-style" in Ruby. That's when you call a method without
an explicit receiver. For example:

puts "Hello world!" # function-style
file.puts "Hello world!" # normal method call

Everything is still a method, this is just a name we use for the call
style.

I only bring it up because it is sometimes referred to in the
documentation for things like private scope and module_function.

James Edward Gray II


dave rose

3/28/2007 12:51:00 PM

0

Jamal Soueidan wrote:
> Thats what I meant, functions is used without the object, and are called
> direct, whereas the methods is used with the object.
>
> puts "this is a function"
>
> File.puts "this is object.method"
>
> Is this how everyone see it or do many people have different opinion on
> this
> subject?

...no! wait a second... isn't a standalone function really an object?

--
Posted via http://www.ruby-....

James Gray

3/28/2007 12:56:00 PM

0

On Mar 28, 2007, at 7:51 AM, Dave Rose wrote:

> Jamal Soueidan wrote:
>> Thats what I meant, functions is used without the object, and are
>> called
>> direct, whereas the methods is used with the object.
>>
>> puts "this is a function"
>>
>> File.puts "this is object.method"
>>
>> Is this how everyone see it or do many people have different
>> opinion on
>> this
>> subject?
>
> ...no! wait a second... isn't a standalone function really an object?

Yes, in Ruby it's always messages being passed to objects. This is
just a name we use for the call style. It's still a method call.

James Edward Gray II

John Joyce

3/28/2007 12:57:00 PM

0

The answer does depend on the language in question!
The difference is small and (arguably) unimportant in Ruby. In C++ or
Objective-C the difference and distinction is more clear and more
important.

Some people would argue the semantic difference between a function
and a method, but in most cases, in most languages they're pretty
much the same.
The main difference is that a method is "part" of an object. It must
understand (respond to) or "have" the method in its definition. (or
inherit the method from an ancestor class)

A function in the strictest sense is a bit of code that is defined
and named and can be called and run by that name. It may take
arguments (parameters) and / or return a value of some sort.

In Ruby almost everything is an object...

Objective-C, has a similar concept of methods and messages to Ruby,
but also has C as it's strict subset, it is a good place to find out
about the differences and similarities.
On Mar 28, 2007, at 9:34 PM, Yamal Khaled Soueidan wrote:

> Thats what I meant, functions is used without the object, and are
> called
> direct, whereas the methods is used with the object.
>
> puts "this is a function"
>
> File.puts "this is object.method"
>
> Is this how everyone see it or do many people have different
> opinion on this
> subject?
>
> On 3/28/07, James Edward Gray II <james@grayproductions.net> wrote:
>>
>> On Mar 28, 2007, at 3:42 AM, SonOfLilit wrote:
>>
>> > Methods are associated with a receiver object.
>> >
>> > Functions are not.
>> >
>> > The only things you can call functions in Ruby are lambdas, blocks,
>> > ... Even then it's not clear whether they are methods or functions.
>> >
>> > Everything else is methods.
>>
>> The above is all true, but we do sometimes refer to calling methods
>> in a "function-style" in Ruby. That's when you call a method without
>> an explicit receiver. For example:
>>
>> puts "Hello world!" # function-style
>> file.puts "Hello world!" # normal method call
>>
>> Everything is still a method, this is just a name we use for the call
>> style.
>>
>> I only bring it up because it is sometimes referred to in the
>> documentation for things like private scope and module_function.
>>
>> James Edward Gray II
>>
>>
>>


Thomas Hafner

4/13/2007 11:58:00 PM

0

John Joyce <dangerwillrobinsondanger@gmail.com> wrote/schrieb <23778091-D3E1-4592-A11E-48DC4DDDBBE4@gmail.com>:

> A function in the strictest sense is a bit of code that is defined
> and named and can be called and run by that name. It may take
> arguments (parameters) and / or return a value of some sort.

A function does not need to be named, e.g.:
lambda { |x| 5 * x } [3]
I'd call this calling/running an anonymous function.

Regards
Thomas