Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes and an additional way for defining singleton methods #25

Merged
merged 3 commits into from Sep 18, 2011

Conversation

mitio
Copy link
Contributor

@mitio mitio commented Sep 17, 2011

No description provided.

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 18, 2011

Nice addition.

@DAddYE
Copy link

DAddYE commented Sep 18, 2011

Another good way in my opinion is:

module Foo
  extend self

  def bar
    # do_some
  end
end

Btw, my flow is: using self.method_name when Ive less than 3 singleton methods, use @mitio way when Im in a class and I've more than 3 singleton methods, use my way extend self when all methods are singleton.

@mitio
Copy link
Contributor Author

mitio commented Sep 18, 2011

Frankly, I don't prefer this way of defining class methods when I have just a few of them. In such cases, I use def self.method_name.

However, I've seen this used in numerous places and I'm not really against it, especially when it comes to defining more than a few class methods. I think it deserves a mention.

For comparison, I ran some checks against Rails's master branch and the rough results are:

  • 494 class methods defined with def self.method
  • 203 class methods defined within class << self, not counting delegate'd ones

Thus, I would definitely say it deserves a mention.

The stats are approximate numbers, I used some rough grep's and some ruby code for them. To be as transparent as possible, here's the code:

# counts def self.method
grep -rI '\bdef self\.' . | wc -l

# couns class << self methods
grep -rIl 'class << self' . | while read fn; do ruby count_class_methods.rb $fn; done

You can find the quick and ugly ruby script I wrote to count class << self class methods here: https://gist.github.com/1225338

bbatsov added a commit that referenced this pull request Sep 18, 2011
Minor fixes and an additional way for defining singleton methods
@bbatsov bbatsov merged commit 51b5d46 into rubocop:master Sep 18, 2011
marocchino pushed a commit to marocchino/ruby-style-guide that referenced this pull request Aug 11, 2015
Minor fixes and an additional way for defining singleton methods
pocke pushed a commit to pocke/ruby-style-guide that referenced this pull request May 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants