Class Methods and Modules
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.
- 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.) - 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. - Create a class method in Model called
find
that takes in an integerid
and return the product located at indexid
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
Ahmad Abbas Zainol
Aug 26, 9:11 PMI have trouble to solve the last problem. How can I solve this?
Learneroo
Aug 26, 9:52 PMDid you do the previous challenges and make sure to implement the
to_s
method correctly for Product?Ahmad Abbas Zainol
Aug 26, 9:55 PMyes.. I already did... but still, I'm stuck and at Model class...
Learneroo
Aug 26, 10:05 PMHint: Make sure your Model's create method creates a new Product.
Ahmad Abbas Zainol
Aug 26, 10:10 PMI've already did that... but maybe there's something wrong with my code... still can't figure out the solution... I'm stuck at find method
Ahmad Abbas Zainol
Aug 26, 10:12 PMyes... finally.... I did manage to solve this problem... the problem with my code, instead writing, product = Product.new(name, description), I wrote product = Model.new(name, description).. thanks for the help...