Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of the "method" method in Ruby?

Tags:

ruby

In Ruby, there is the "method" method, which creates a method object that you can then treat as a Proc. This is useful if you wish to do metaprogramming:

def foobar(method_as_a_string)
   2.method(method_as_a_string).call(2)
end

foobar("+")
=> 4

foobar("-")
=> 0

However, normally, if you want to do such meta-programming, you would use send instead of method...

def foobar(method_as_a_string)
   2.send(method_as_a_string, 2)
end

foobar("+")
=> 4

foobar("-")
=> 0

What is the point of the "method" method then in Ruby?

like image 477
Left SE On 10_6_19 Avatar asked Jan 30 '17 16:01

Left SE On 10_6_19


People also ask

What does method do in Ruby?

A method in Ruby is a set of expressions that returns a value. With methods, one can organize their code into subroutines that can be easily invoked from other areas of their program. Other languages sometimes refer to this as a function. A method may be defined as a part of a class or separately.

What happens when you call a method in Ruby?

In ruby, the concept of object orientation takes its roots from Smalltalk. Basically, when you call a method, you are sending that object a message. So, it makes sense that when you want to dynamically call a method on an object, the method you call is send . This method has existed in ruby since at least 1.8.

Can you define a method within a method in Ruby?

In short: no, Ruby does not support nested methods.

What is method name in Ruby?

Ruby allows method names and other identifiers to contain such characters.) Method names may contain letters, numbers, an _ (underscore or low line) or a character with the eight bit set. Method names may end with a ! (bang or exclamation mark), a ? (question mark) or = equals sign.


Video Answer


2 Answers

You can pass a method result to a block, and it will call the method passing the iteratee as arguments.

For example, this wouldn't work:

[1,2,3].each(&:puts)

because it's calling 1.puts, 2.puts, etc. But this would:

[1,2,3].each(&method(:puts))

Another example:

arr, result = [1,2,3], []
arr.each &result.method(:push)
result # => [1,2,3]

Another useful thing is using it to find the source location of a method:

Someclass.method(:some_method).source_location
# => this will give you the file & line where it's defined

This is a very helpful trick when you're working with gems and want to quickly glance at their source code

like image 117
max pleaner Avatar answered Oct 18 '22 22:10

max pleaner


A Method object provides for all sorts of introspection and metaprogramming, including:

  1. Getting the arity or parameters for the method
  2. Getting the source code location for the method, which is useful in debugging or when writing tools that interact or inspect the source of the application that's running
  3. Getting the receiver of the method

How and what you use those powers for is up to you.


For Cary, below:

"cat".method(:count).receiver
# => "cat"
like image 25
coreyward Avatar answered Oct 18 '22 20:10

coreyward