Coding as a Craft
  • Introduction
    • Learning objectives
  • Your first website
  • ATM Challenge - Ruby basics
    • Step 1
    • Step 2
    • Step 3
    • Step 4
    • Step 5
    • Step 6
    • Step 7
    • Step 8
    • Step 9
    • Step 10
  • Library challenge - Advanced Ruby
    • Important topics
  • Javascript Introduction
    • Variables, objects and arrays
    • Comparisons and Manipulations
    • Javascript Sample Problems
    • Defining Functions
    • Prototypes & Classes
    • Miscellaneous
  • BMI Challenge - JavaScript basics
    • Jasmine - Set up
    • First tests
    • The calculator
    • The Document-Object Model
    • Web interface
    • Acceptance tests
    • Moving on
  • Fizz Buzz in JavaScript
    • NodeJS
  • Checkout challenge
  • Open Weather Challenge
  • SlowFood challenge - OO & TDD
    • Step 1 - Setting up the project
    • Step 2 - Focus on the user experience
    • Step 3 - Entity Relationship Diagrams
    • Step 4 - Implementing the core features
    • Step 5 - Working with the database
    • Step 6 - Working with BDD
    • Extra - Setting up RSpec & Cucumber
  • Ruby On Rails introduction
  • Static Website with Middleman
    • Week lab
    • Setup Middleman
    • HAML - HTML abstraction markup language
    • SASS
    • Accessing data
    • Partials
    • Deploy to Github pages
  • BDD with Rails
    • Exercise - Implement extra features
  • Rails Messaging
    • Working with Legacy Code
    • Tips and Tricks
  • Mid Course Project
    • Project Schedule
  • Going mobile with Ionic
    • Setup
    • Javascript Modules
    • Introduction to Angular
    • Getting to Know Ionic - BMI Challenge
    • Ionic unit and e2e testing
  • The Cooper test challenge
    • The logic
    • The Back-end
    • The Client
    • Connecting the dots
    • Saving and retrieving data
    • Display charts
    • Wrapping up
    • Results tables
  • News Room Challenge
    • Main features
    • Design Sprint
    • Pivotal Tracker
    • Guides
      • i18n with Ruby on Rails
      • Rails 5.2 Scaffold
      • Attachments with Active Storage
      • Encrypted Credentials in Rails 5.2
      • Role-Based Authorization
      • Rendering JSON objects in Rails
      • Testing Ionic Applications
  • SlowFood API - API first or second?
  • Extras
    • Naming Standards
    • Classes vs Modules
    • Code structure
    • Bower
    • Code Review Instructions
    • About README's
    • MVC
    • Three-Tier Architecture
    • Rails Scaffold
    • Tailwind css with Rails
  • Career
    • General Personality
    • Agile Mindset
    • Basic Ruby
    • Advanced Ruby
    • Databases
    • SOLID Principles
  • Sinatra & SlowFood
    • Sinatra - an introduction
    • Start small
    • More Hello World
  • Ember
    • Hello World
Powered by GitBook
On this page
  • Class - methods
  • Module - methods
  1. Extras

Classes vs Modules

Classes are about objects; Modules are about functions.

Modules are about providing methods that you can use across multiple classes - think about them as "libraries". A Module is just a collection of methods and constants and comes in handy at times when you want to group things together that don't naturally form a Class. Classes are also a collection of methods and constants, but with the added functionality of being able to be instantiated. A Module cannot be instantiated.

Class

Module

Instantiation

can be instantiated

can not be instantiated

Usage

object creation

mixin facility, provide a namespace.

Superclass

Module

Class

Methods

class methods and instance methods

module methods and instance methods

Inheritance

can inherit behaviour and can be base for inheritance

no inheritance

Inclusion

cannot be included

can be included in classes and modules by using the include command (includes all instance methods as instance methods in a class/module)

Extension

can not extend with extend command (only with inheritance)

module can extend instance by using extend command (extends given instance with singleton methods from module)

Class - methods

Class methods are methods that are called on a class and instance methods are methods that are called on an instance of a class.

class FooClass
  def self.bar
    'class method'
  end

  def baz
    'instance method'
  end
end

FooClass.bar # => "class method"
FooClass.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class

FooClass.new.baz # => instance method
FooClass.new.bar # => NoMethodError: undefined method ‘bar’ for #<Foo:0x1e820>

Module - methods

The methods in a module may be instance methods or module methods. You can include methods in your module that you can be both functions or included by one or several Classes and used as instance methods. Instance methods appear as methods in a class when the module is included, module methods do not. Conversely, module methods may be called without creating an encapsulating object, while instance methods may not.

There are several ways you can create methods in a Module. Let's say you want to be able to do

FooModule.foo(some_value)

You can define the foo method like this

module FooModule

  def self.foo(bar)
    bar
  end
end

Or like this...

module FooModule
  extend self

  def foo(bar)
    bar
  end
end

...or like this.

module FooModule

  module_function
  def foo(bar)
    bar
  end
end
PreviousNaming StandardsNextCode structure

Last updated 6 years ago