Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Sep 9 ’11

Ways to define class methods

There are different ways to create class methods in Ruby. These three are probably the most common ones. They all do the same.

class Blog

  def self.foo
    puts 'I am a class method'
  end

  def Blog.bar
    puts 'I am a class method, too'
  end

  class << self
    def foobar
      puts 'I am another class method'
    end
  end

end

There are even more ways to define class methods. Check out these two post for more:

1 note 0 comments

  1. rubyquicktips posted this

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus