Accessing Class Methods

Please consider the following snippet of code:

class Parent
def self.parent_greeting
return ‘Hello, from the Parent class.’
end
end
class Child < Parent
def self.child_greeting()
return ‘Hello, from Child class.’
end
def get_greeting()
puts(self.class.child_greeting())
puts(Parent.parent_greeting())
end
end

If I instantiate the Child class into my_greeting, then:

my_greeting.class.parent_greeting() => Hello, from the Parent class.

and

my_greeting.class.child_greeting() => Hello, from Child class.

My question is: What are these ‘class’ methods that show up in my
method calls to the object and in the get greeting method?

I have a hunch that the question is answered in the ‘Disambiguation’
section of http://www.jimmycuadra.com/posts/self-in-ruby; but, I wasn’t
able to understand it. Is the answer to my question something that I’m
going to be able to grasp; or, will I need to wait until my skills
improve?

Thanks for any input.

     ... doug

Module or Class methods are simply methods which don’t need or belong to
a particular instance. In general they’ll be methods which don’t need to
reference the internal state of an object. The most commonly used (but
not commonly altered) Class method would Class.new

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs