Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8934815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:52:02+00:00 2026-06-15T09:52:02+00:00

Can anyone explain to me what the meaning of adding self to the method

  • 0

Can anyone explain to me what the meaning of adding self to the method definition is? Is it similar to the this keyword in java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:52:03+00:00Added an answer on June 15, 2026 at 9:52 am

    Contrary to other languages, Ruby has no class methods, but it has singleton methods attached to a particular object.

    cat = String.new("cat")
    def cat.speak
        'miaow'
    end
    cat.speak #=> "miaow" 
    cat.singleton_methods #=> ["speak"] 
    

    def cat.speak creates a singleton method attached to the object cat.

    When you write class A, it is equivalent to A = Class.new :

    A = Class.new
    def A.speak
        "I'm class A"
    end
    A.speak #=> "I'm class A" 
    A.singleton_methods #=> ["speak"] 
    

    def A.speak creates a singleton method attached to the object A. We call it a class method of class A.

    When you write

    class A
        def self.c_method
            'in A#c_method'
        end
    end
    

    you create an instance of Class(*). Inside the class definition, Ruby sets self to this new instance of Class, which has been assigned to the constant A. Thus def self.c_method is equivalent to def cat.speak, that is to say you define a singleton method attached to the object self, which is currently the class A.

    Now the class A has two singleton methods, that we commonly call class methods.

    A.singleton_methods
     => ["c_method", "speak"] 
    

    (*) technically, in this case where A has already been created by A = Class.new, class A reopens the existing class. That’s why we have two singleton methods at the end. But in the usual case where it is the first definition of a class, it means Class.new.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain this code to me? Whats the meaning and how to use
Can anyone explain the meaning of this code? if (data.result) { $('ul#intlist').append(data.content); }
Can anyone please explain what is the meaning of this code for ARM? __asm__
can anyone explain the meaning behind this regular expression? '/<div align=left><a href=(.*?)>(.*?)<\/a><\/div>/s' from what
can anyone explain the meaning/reason of this adsheet warning please? Nov 23 09:24:06 unknown
Can anyone please explain me the meaning of implementing Map class and how should
Can anyone explain to me why this program: for(float i = -1; i <
Can anyone explain why this code: {% form_theme form _self %} {% block avo_gallery_upload_widget
I am new to perl. Can anyone please explain the meaning of // operator
Can anyone, please, explain to me in very simple terms what a "method" is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.