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

  • Home
  • SEARCH
  • 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 3681410
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:41:10+00:00 2026-05-19T03:41:10+00:00

In the Ruby Standard Library we have the Singleton class: http://ruby-doc.org/stdlib/libdoc/singleton/rdoc/index.html We can make

  • 0

In the Ruby Standard Library we have the Singleton class:
http://ruby-doc.org/stdlib/libdoc/singleton/rdoc/index.html

We can make any class a singleton by including this class inside. I just rarely see this used. When would it make sense to use this Singleton class vs. just using plain old class methods – also known as singleton methods?

Said in another way: Which Singleton coding-convention are the best and why? Here are three ways I could think of:

require 'singleton'
class Foo
  include Singleton
  # method definitions go here...
end

Foo.instance.do_something!

Versus

class Foo
  class << self
    # method definitions go here...
  end
end

Foo.do_something!

Versus

module Foo
  class << self
    # method definitions go here...
  end
end

Foo.do_something!
  • 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-19T03:41:10+00:00Added an answer on May 19, 2026 at 3:41 am

    WARNING: Opinions ahead!


    If you just need a single object, just use a single object:

    class << (Foo = Object.new)
      # method definitions go here...
    end
    
    Foo.do_something!
    

    Modules are for sharing behavior between objects. Classes are factories for objects. Note the plural: if you have only one object, you need neither a facility for sharing behavior nor a factory for producing multiple copies.

    Whether or not this is considered idiomatic Ruby depends largely on which “Ruby” you are talking about: are you talking about Ruby as used by Rails programmers, Ruby as used by Smalltalk programmers, Ruby as used by Java programmers or Ruby as used by Ruby programmers? There are significant differences in the styles used by these different communities.

    For example, old time Rubyists like David Alan Black tend to start always with just objects and singleton methods. Only if they later on discover duplicated behavior between objects will they extract that behavior into a mixin and extend the objects with it. (Note that at this stage, there still aren’t any classes!)

    Classes are created, again, only by refactoring, not by upfront design, when and only when there is duplicated structure between objects.

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

Sidebar

Related Questions

Does Ruby's standard library have a priority queue implementation?
In short, is the standard library Logger class in Ruby thread-safe? Only useful info
Ruby can add methods to the Number class and other core types to get
I'm working off these instructions: http://github.com/dcparker/ruby-gmail From the home directory I do a standard
I have a finished Ruby project that has the standard structure for a multiple
Ruby's standard popen3 module does not work on Windows. Is there a maintained replacement
Say I'm writing some ruby code and I want to use the standard Date
Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue. Why do we have two?
Can I replace an executable (accessed via a system call from ruby) with an
I have to implement some code in Ruby based on Java implementation that was

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.