Class Methods and Modules


Collapse Content

Class methods

class methods are called on the overall class instead of a specific Object. They are created with the self keyword (to refer to the class itself):

class Dog     
  def self.greet(message)
    "Woof #{message} woof!"
  end    
end

Without creating any specific dogs, you can use Dog's greet method:

Dog.greet("hello") #=> Woof hello woof!

All classes come with a class method called .new. This is how new instances of a class are created:

doggy = Dog.new #=> # (new Dog instance)

Class methods cannot be called on instances; Ruby will raise an error if you try it:

doggy.greet("hello") 
#=> NoMethodError: undefined method `greet' for #<Dog:0x001016>

Note that class Dog should be used to create Dog instances. If you just want to groups multiple 'class' methods together, use a module instead.

Modules

Modules in Ruby are used to bundle groups of methods (and constants) together under one name so they can be easily re-used.

For example, Ruby's Math module provides many useful math methods:

 Math.sqrt(81)  #=> 9.0
 Math.hypot(3,4)  #=> 5.0 

Math.sqrt returns the square root of a number.
Math.hypot returns the hypotenuse of a right-angled triangle with those sides.

Modules are created like classes, but with the module keyword instead of class:

module Noises
  def self.quack
    puts "Quack!"
  end
end

You can now call the methods from the module on other code:

Noises.quack

prints out:

quack

Requiring files
If you're writing code in one file and want to use a module from a different file, you'll need to import the file with a require [fileName] statement. For example, this code will give you access to the module(s) located in the foo.rb file:

 require "./foo.rb"

(The ./ at the beginning tells ruby to look in the same folder as your current file.)

Modules in Ruby can also be used to mixin functions into actual class instances. You don't need to worry about mixins now, but see Modules by example if you're interested.

Challenge

Copy your previous Product code over, and create a new Model class for storing multiple products.

  1. Create the class Model and create an array @products at the beginning of it . (This array will be used instead of a real database used in Rails applications.)
  2. Create a class method in Model called create that creates a product and adds it to the end of @products. create should take in parameters for the name and description of the product it creates.
  3. Create a class method in Model called find that takes in an integer id and return the product located at index id in the @products array.

If you created the Model correctly, the following code will cause it to create a Cow product and then return that product:

Model.create("Cow", "moos") #creates a Cow product and adds it to @products
Model.find(0) #returns the product located at index 0

Challenge

Create a Model class as described above.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Challenge

The Ruby class Time is used for creating time/date instances. It provides a convenient class method .now for creating a new Time instance set to the current time. Create a new time called start set to the current time.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

Contact Us
Sign in or email us at [email protected]