Jegan
Jegan

Reputation: 194

Object Oriented Programming in ruby

Is method an object in Ruby? My friend asked me this question.I read about this in website.But still I didn't understand it.Can anyone help me?

Upvotes: 3

Views: 107

Answers (2)

sawa
sawa

Reputation: 168071

There seems to be a confusion here due to ambiguity of the term method. Method in the most ordinary sense is not an object. In the following:

"foo".upcase

the method upcase is applied to an object "foo", but upcase is not an object, as can be seen by the fact that it cannot stand alone:

upcase # => error

(Do not confuse this with when it can be considered that the receiver is omitted).

However, there is a class Method, whose instances correspond to methods, and are objects. They may also be called methods, but that is not the normal usage of the term method.

Upvotes: 5

Aetherus
Aetherus

Reputation: 8888

No, they are not.

Methods themselves are a language structure of Ruby, and they are not objects. But there is a class Method, whose instances represent methods, and can be called using Method#call.

Also, there is another kind of instances - instances of class UnboundMethod, which represent methods that are detached from specific objects. They can't be called directly, but can be used in many different ways.

If you are looking for something like Javascript's functions, then procs and lambdas are what you want.

Upvotes: 4

Related Questions