# Implementing the Self-Shunt testing pattern

**URL:** <https://www.ruby-forum.com/t/implementing-the-self-shunt-testing-pattern/53651>\
**Category:** Ruby\
**Created:** [February 13, 2006, 6:16am UTC](https://www.ruby-forum.com/t/implementing-the-self-shunt-testing-pattern/53651 "2006-02-13T06:16:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [February 13, 2006, 6:16am UTC](https://www.ruby-forum.com/t/implementing-the-self-shunt-testing-pattern/53651/1 "2006-02-13T06:16:56Z")

</div>

I just stumbled across the Self-Shunt unit testing pattern[0] and  
decided, in my nuby quest to learn more about Ruby, to implement it in  
Ruby. It’s simple enough thanks to duck-typing. Almost ridiculously so,  
in fact.

My question, though, is: does the way I implemented this go against an  
established ruby idiom or violate “the ruby way” in any way? If so I’d  
love to hear it.

Thanks!  
-dave

[0] [Vacation Rental Hacks](http://www.objectmentor.com/resources/articles/SelfShunPtrn.pdf)

===== self\_shunt.rb  
require ‘test/unit’

# Implements self-shunt pattern in Ruby.

# Self-shunt detailed at:

[http://www.objectmentor.com/resources/articles/SelfShunPtrn.pdf](http://www.objectmentor.com/resources/articles/SelfShunPtrn.pdf)

class Display  
def display\_item(item)  
puts “The REAL display is showing: #item”  
# God only knows what other processing might take place here …  
end  
end

class Scanner

# accepts any object having a #display\_item(item) method

# hooray duck typing 🙂

def initialize(display)  
@display = display  
end  
def scan(item)  
@display.display\_item(item)  
end  
end

# completely pointless class to demonstrate the idea

class Item  
def initialize(name)  
@name = name  
end  
def to\_s  
@name  
end  
end

# first test the self-shunt

class ScannerTest \< Test::Unit::TestCase  
def test\_scan  
scanner = Scanner.new(self)  
item = Item.new(“test item”)  
scanner.scan(item)  
assert\_equal(“test item”, @displayed\_item.to\_s)  
end  
def display\_item(item)  
@displayed\_item = item  
end  
end

# now do the real thing

def main # habits die hard… 🙂  
display = Display.new  
scanner = Scanner.new(display)  
scanner.scan Item.new(“now we are displaying the REAL item”)  
2.times {puts} # to pad between this output and the test output  
end

main

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [February 13, 2006, 6:31am UTC](https://www.ruby-forum.com/t/implementing-the-self-shunt-testing-pattern/53651/2 "2006-02-13T06:31:13Z")

</div>

DÅ?a Pondelok 13 FebruÃ¡r 2006 06:16 Dave C. napÃ­sal:

> -dave
> 
> [0] [http://www.objectmentor.com/resources/articles/SelfShunPtrn.pdf](http://www.objectmentor.com/resources/articles/SelfShunPtrn.pdf)

I -so- needed this PDF - self-shunt is the one thing that really  
confused me  
while skimming through Beck’s TDD:BE. Now if I can figure out how to do  
this  
in Java without getting a compiler induced headache, my job will be so  
much  
more fun…

> main

Weakling 😜 You forgot an if $0 == **FILE** , or however that idiom I  
never use  
anyway goes. Next thing we know, you’re hacking the interpreter to give  
you  
warnings about mismatched signedness of variables…

Anyways, thanks a lot for the code snippet, I have something to grok as  
relaxation at last.

David V.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [February 13, 2006, 7:27am UTC](https://www.ruby-forum.com/t/implementing-the-self-shunt-testing-pattern/53651/3 "2006-02-13T07:27:58Z")

</div>

> I -so- needed this PDF - self-shunt is the one thing that really confused me  
> while skimming through Beck’s TDD:BE. Now if I can figure out how to do this  
> in Java without getting a compiler induced headache, my job will be so much  
> more fun…

Yeah, I remember seeing this pattern a while back somewhere else. Head  
scratches and muttered “wtf” abounded. This made it ridiculously clear.

> Anyways, thanks a lot for the code snippet, I have something to grok as  
> relaxation at last.

Sweet.
