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
It worked instantly as intended. ;)