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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:57:05+00:00 2026-05-19T01:57:05+00:00

Possible Duplicates: class << self idiom in Ruby Can someone please explain class <<

  • 0

Possible Duplicates:
class << self idiom in Ruby
Can someone please explain class << self to me?

I would like to know what does class << self statement mean in a model class? And how does the statement inside it differ from those outside from it. For example:

class Post < ActiveRecord::Base

  class << self
     def search(q)
          # search from DB
     end
  end
   def search2(qq)
         # search from DB
   end
end

What does class << self mean?

What are the differences between method search(q) and search2(qq) ?

  • 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-19T01:57:05+00:00Added an answer on May 19, 2026 at 1:57 am

    That is the same as

    class Post < ActiveRecord::Base
    
      def self.search(q)
        # Class Level Method
        # search from DB
      end
    
      def search2(qq)
        # Instance Level Method
        # search from DB
      end
    end
    

    Class methods work on the class (e.g. Post), instance methods works on instances of that class (e.g. Post.new)

    Some people like the class << self; code; end; way because it keeps all class level methods in a nice block and in one place.

    Others like to prefix each method with self. to explicitly know that is a class method not an instance method. It’s a matter of style and how you code. If you put all class methods in a block like class << self, and this block is long enough, the class << self line might be out of your editor view making it difficult to know that you are in the class instance block.

    On the other hand, prefixing each method with self. and intermixing those with instance methods is also a bad idea, how do you know all the class methods while reading your code.

    Pick an idiom which you prefer for your own code base but if you work on an open source project or you collaborate on someone else’s code, use their code formatting rule.

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

Sidebar

Related Questions

Possible Duplicate: class << self idiom in Ruby I have a quick Ruby question.
Possible Duplicate: Why XML-Serializable class need a parameterless constructor Does anyone know if it
Possible Duplicate: Python init and self what do they do? Can anyone explain what
Possible Duplicate: What does ||= mean in Ruby? class Person attr_accessor :name class <<
Possible Duplicate: Why does PHP 5.2+ disallow abstract static class methods? Why can't you
Possible Duplicates: What does this mean in jquery $('#id', javascript_object); What does $(''class for
Possible Duplicates: How does delete[] “know” the size of the operand array? ( POD
Possible Duplicate: Creating an abstract class in Objective C In Java I like to
Possible Duplicate: Difference between class property mVar and instance variable self.mVar I am new
Possible Duplicate: Why does defining getitem on a class make it iterable in python?

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.