0

A ruby method is an Object. For example:

def method
  'does something'
end
method.is_a? Object #=> true

def method; end 
method.is_a? Object #=> true

Why is a method a Object?

sawa
  • 165,429
  • 45
  • 277
  • 381
Gabriel Mesquita
  • 2,271
  • 1
  • 20
  • 30
  • 5
    Everything is an object. You can’t get very far into Ruby-land without hearing that phrase. It’s true though, everything in Ruby is an object. The interesting thing is how those objects are linked together and classified. – Roman Kiselenko Sep 27 '18 at 20:05
  • Possible duplicate of [Methods in Ruby: objects or not?](https://stackoverflow.com/questions/2602340/methods-in-ruby-objects-or-not) – orde Sep 27 '18 at 20:23
  • 3
    You are not inspecting methods in your samples, but objects returned by them - in first case a `String`, in second - `nil`. Try `method.class` to check that, the `method` is called and `is_a?` is called on it's return value. Methods have class `Method`. – Bartosz Pietraszko Sep 27 '18 at 20:29

3 Answers3

2

method.is_a?(Object)

can be rewritten as

res = method
res.is_a?(Object)

So you call a method and ask if its result -- a String instance -- is an Object. For sure it is.

It can be easily verified with:

method.is_a?(String) # also returns true

Update:

If you want to check if a method itself an Object or not, you can do it with this code snippet:

def method2
  "some string"
end

method2_ref = Object.method(:method2) # #<Method: Object.method2>
method2_ref.is_a? Object # true
method2_ref.is_a? String # false

As it is mentioned in the comments, Method instance is also an Object.

Ilya Konyukhov
  • 2,666
  • 1
  • 12
  • 21
2

Like Ilya Konyukhov wrote, calling method.is_a?(Object) checks the return value of calling method, which would be "does something" and nil respectively - both of which are Objects.

Rubys methods are not objects per se but you can get an object representation of them using the Object#method method.

The funny part of your specific example is, that you unfortunately named your test method "method", thus overwriting your selfs method method.

Take a look at this example, where I named my method "test" instead:

def test; end

test.class.name          => "NilClass"

method(:test).class.name => "Method"

As you see, test is calling the method and everything after (e.g. is_a? or class.name) is called on the return value.

method(:test), however, returns an object representation (of class Method) of the method itself.

Alexander Presber
  • 6,429
  • 2
  • 37
  • 66
  • Your second paragraph is misleading. What is normally meant by "method" and an instance of `Method` class are different things. The OP meant the former, not the latter. – sawa Sep 28 '18 at 03:38
  • 1
    @sawa changed my answer so that it does not imply that methods are identical with instances of the class `Method`. – Alexander Presber Oct 01 '18 at 10:40
1

"Ruby' s methods are not objects in the way that strings, numbers and arrays are" (The Ruby Programming Language, Flanagan and Matsumoto, page 176). OP' s method name "method" is unfortunate; it makes it almost impossible to explain anything. If you need to treat a method as an object, there is the method method, which results in an instance of Class Method.

steenslag
  • 79,051
  • 16
  • 138
  • 171