Implementing the Null Object Pattern in Ruby

4
943

This coding exercise will test your ability to implement a basic version of the Null Object Pattern in Ruby.

Summary

Implement a class that utilizes the Null Object pattern in Ruby.

Exercise File

Code File

Exercise Description

Build a class that can receive any method call and return the object instead of a no method error.

Real World Usage

This is a common pattern to follow in Rails development, specifically when working with guest users in an application.

Solution

Can be found on the solutions branch on github.

4 COMMENTS

  1. You’ll get Rubocop errors with just that so try adding this

    def method_missing(name, *args, &block)
    if respond_to?(name)
    self
    else
    super
    end
    end

  2. I just don’t know what format to use here for code. Is it four spaces indentation like stackoverflow? Let’s see
    def method_missing(name, *args, &block)
    if respond_to?(name)
    self
    else
    super
    end
    end

LEAVE A REPLY

Please enter your comment!
Please enter your name here