8

I read this:

Let’s start with a simple Ruby program. We’ll write a method that returns a cheery, personalized greeting.

def say_goodnight(name)
    result = "Goodnight, " + name
    return result
end

My understanding is that a method is a function or subroutine that is defined in a class, which can be associated to a class (class method) or to an object (instance method).

Then, how can that be a method if it was not defined within a class?

1
  • 3
    Recall that methods return the last value calculated. For that reason, this method would normally be written def say_goodnight(name); "Goodnight, " + name; end. Also, you'd often see the second line written, "Goodnight, #{ name }" or "Goodnight, %s" % name. Commented Jan 4, 2015 at 19:27

1 Answer 1

15

When you define a function in Ruby at the global scope in this way, it technically becomes a private method of the Object class, which is the base class that everything inherits from in Ruby. Everything in Ruby is an object, so it is indeed true that you have defined a method.

def say_goodnight(name)
    result = "Goodnight, " + name
    return result
end

Object.private_methods.include? :say_goodnight
=> true

Because it is defined as a method with private visibility on Object, it can only be called inside objects of the class on which it's defined or subclasses. So why does it appear to be available globally?

Basically, the Ruby program itself defines an instance of Object called main, which serves as the top-level scope where your method was defined. So if you think of your program as running inside main (which is an Object) its private methods are available for use.

# In irb:
self
=> main
self.class
=> Object
self.private_methods.include? :say_goodnight
=> true

Addendum: This answer which further explains how main is defined and implemented.

Update for Ruby >= 2.3

Noted in the comment thread, later versions of Ruby would define the method Object#say_goodnight in this example with public visibility rather than private. This behavior appears to have changed between Ruby 2.2.x and 2.3.x, but does not affect method exposure.

3
  • Being private has nothing to do with inheritance.
    – sawa
    Commented Jan 4, 2015 at 21:08
  • when I run these code in irb with ruby-2.6.5, the method is defined as public
    – lfx_cool
    Commented Dec 10, 2019 at 15:55
  • 1
    @lfx_cool I can confirm that. I'll have to investigate when the change happened and update the answer accordingly Commented Dec 10, 2019 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.