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 692701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:40:40+00:00 2026-05-14T02:40:40+00:00

In Ruby, how do you call a class method from one of that class’s

  • 0

In Ruby, how do you call a class method from one of that class’s instances? Say I have

class Truck
  def self.default_make
    # Class method.
    "mac"
  end

  def initialize
    # Instance method.
    Truck.default_make  # gets the default via the class's method.
    # But: I wish to avoid mentioning Truck. Seems I'm repeating myself.
  end
end

the line Truck.default_make retrieves the default. But is there a way of saying this without mentioning Truck? It seems like there should be.

  • 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-05-14T02:40:40+00:00Added an answer on May 14, 2026 at 2:40 am

    Rather than referring to the literal name of the class, inside an instance method you can just call self.class.whatever.

    class Foo
        def self.some_class_method
            puts self
        end
    
        def some_instance_method
            self.class.some_class_method
        end
    end
    
    print "Class method: "
    Foo.some_class_method
    
    print "Instance method: "
    Foo.new.some_instance_method
    

    Outputs:

    Class method: Foo
    Instance method: Foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ruby, can one object destroy another? For example: class Creature def initialize @energy
I have a class with this method: def telecom_info Rails.cache.fetch("telecom_info_for_#{ref_num}", :expires_in=> 3.hours) do info
How do I call shell commands from inside of a Ruby program? How do
I want to send email from a Ruby application. Is there a call in
I have a shoulda macro/method that tests controller responses for XPath conditions like so:
How can I mock the initialize method on a ruby class? I'm doing some
I want to intercept method calls on a ruby-class and being able to do
I have a more conceptual question in Rails... or Ruby for that matter: Is
Not sure on my Ruby syntax here. I want to define a method that
Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue. Why do we have two?

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.