Skip to content

Instantly share code, notes, and snippets.

@npassaro
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npassaro/acd5ae47f72d3861246f to your computer and use it in GitHub Desktop.
Save npassaro/acd5ae47f72d3861246f to your computer and use it in GitHub Desktop.
Decorator that kind of implements Null Object pattern
module Utils
class StringDecorator
def initialize(string)
@string = string
end
def =~(regex)
self.to_s =~ regex
end
def nil?
@string.nil?
end
def to_s
@string
end
def string
@string
end
def method_missing(name,*args,&block)
if self.nil?
self
elsif self.string.respond_to? name
@string = @string.send(name,*args)
self
else
super
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment