%w(hashrockets spaceships bangbangs)

> non-optimized bits & pieces <

3 Ways to Define Class Methods

There are 3 ways to create class methods in ruby:

Probably when you just need one
1
2
3
4
5
6
7
class Thing
  def self.all
    :everything
  end
end

Thing.all # >> :everything
Probably when you need several
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Thing
  class << self

    def self.all
      :everything
    end

    def self.some
      :something
    end

  end
end

Thing.all  # >> :everything
Thing.some # >> :something
Probably when you are going bananas
1
2
3
4
5
6
7
class Thing
  def Thing.all
    :everything
  end
end

Thing.all # >> :everything

If you work with me & u use the last one, i’ll hunt u down & haunt you :P

ruby, tips

Comments