In ruby ​​there is a difference between method and function

Possible duplicate:
Ruby Functions and Methods

I am just reading some ruby ​​documentation, and I seem to use the term function and method in an interchangeable way, I wanted to know if there is any difference?

Im docs look at this as a function:

def saysomething() puts "Hello" end saysomething 

and this method:

 def multiply(val1, val2 ) result = val1 * val2 puts result end 

it may be something semantic, but I wanted to check

Jt

+4
source share
1 answer

There are no two separate concepts for methods and functions in Ruby. Some people still use both terms, but, in my opinion, using the “function” when it comes to Ruby is incorrect. There are no executable code fragments that are not defined on objects, because in Ruby there is nothing that is not an object.

As Dan pointed out, there is a way to call methods that make them look like functions, but the underlying thing is still a method. In fact, you can see this in IRB using the method method .

 > def zomg; puts "hi"; end #=> nil > method(:zomg) #=> #<Method: Object#zomg> > Object.private_instance_methods.sort #=> [..., :zomg] # the rest of the list omitted for brevity 

So, you can see, the zomg method is an instance method of an Object and is included in the list of objects of private instance methods.

+6
source

Source: https://habr.com/ru/post/1384806/


All Articles