nonopolarity
nonopolarity

Reputation: 151126

In Ruby, how to tell two methods or two functions are the same?

For example, to check whether

CGI::parse == CGI.parse

(after doing require "cgi"), how can it be done?

Upvotes: 0

Views: 328

Answers (1)

Tom Lord
Tom Lord

Reputation: 28305

CGI.parse is not valid code, since you are calling the method (without arguments, and the method requires one).

However, in ruby, (almost) everything is an object -- which includes methods! (A method is an instance of the class: Method.)

You can access the CGI::parse method via: CGI.method(:parse).

Then, to check that two methods are equal, you can use the == method on the Method class:

CGI.method(:parse) == CGI.method(:parse) #=> true

From the ruby documentation:

Two method objects are equal if they are bound to the same object and refer to the same method definition and their owners are the same class or module.

Upvotes: 1

Related Questions