Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@andrewsardone
Created March 23, 2013 15:35
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 andrewsardone/5228121 to your computer and use it in GitHub Desktop.
Save andrewsardone/5228121 to your computer and use it in GitHub Desktop.
require 'delegate'
require 'test/unit'
# http://www.saturnflyer.com/blog/jim/2013/03/21/ruby-delegate-rb-secrets/
class Displayer < SimpleDelegator
# alias_method from post originally out of order:
# alias_method :__getobj__, :object
alias_method :object, :__getobj__
def name_with_location
"#{object.name} of #{object.city}"
end
end
class TestDisplayer < Test::Unit::TestCase
def test_name_with_location
jim = Struct.new(:name, :city).new("Jim", "Some City")
displayer = Displayer.new(jim)
assert_equal "Jim of Some City", displayer.name_with_location
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment