The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Null Object Pattern

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Florian Frank

Posts: 48
Nickname: ffrank
Registered: Dec, 2005

Florian Frank is a humanoid life-form, living on the third planet of the solar system.
Null Object Pattern Posted: May 3, 2006 2:35 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Florian Frank.
Original Post: Null Object Pattern
Feed Title: The Rubylution: Tag Ruby
Feed URL: http://rubylution.ping.de/xml/rss/tag/ruby/feed.xml
Feed Description: Starts… Now.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Florian Frank
Latest Posts From The Rubylution: Tag Ruby

Advertisement

This is a useful pattern to stop worrying about nil checks and the like or to unit test with partially mocked objects:

NULL = Class.new do
  def method_missing(*)
    NULL
  end
end.new

NULL now responds to each unknown method call by returning itself.

In my case this yielded some nice syntax to disable error logging instead of writing to a log file in a DSL:

def null
  NULL
end
# ...
error_to null # in DSL

It worked instantly as intended. ;)

Read: Null Object Pattern

Topic: Geodetic Coordinate Systems Previous Topic   Next Topic Topic: Adventures in technical book photography

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use