# Why startup and shutdown is not being called?

**URL:** <https://www.ruby-forum.com/t/why-startup-and-shutdown-is-not-being-called/210066>\
**Category:** Ruby\
**Created:** [July 29, 2011, 4:12pm UTC](https://www.ruby-forum.com/t/why-startup-and-shutdown-is-not-being-called/210066 "2011-07-29T16:12:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 29, 2011, 4:12pm UTC](https://www.ruby-forum.com/t/why-startup-and-shutdown-is-not-being-called/210066/1 "2011-07-29T16:12:34Z")

</div>

startup and shudown method is being called if it is inside the one class  
only. But if i put that into module and include this module in another  
class as shown below startup and shutdown method is not being called.  
module BaseClass  
class \<\< self  
def startup  
p “startup”  
end

```
def shutdown
  p "shutdown"
end

```

end

def setup  
puts “setup called”  
end

def teardown  
puts “teardown called”  
end

def cleanup  
p “cleanup”  
end  
end

DemoTest1.rb

require ‘rubygems’  
gem ‘test-unit’  
require ‘test/unit’  
require “BaseClass”

class DemoTest1 \< Test::Unit::TestCase  
include BaseClass

def test\_third()  
puts “third”  
end  
end

now if i execute the DemoTest1.rb I am getting below output  
Started  
setup called  
third  
“cleanup”  
teardown called

startup and shutdown method is not being called. And i don’t know why??  
any idea guys ???

---

<div class="post-metadata">

**Author:** ![gaurang033](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f8ce5/32.png) [@gaurang033](https://www.ruby-forum.com/u/gaurang033)\
**Post date:** [July 29, 2011, 4:35pm UTC](https://www.ruby-forum.com/t/why-startup-and-shutdown-is-not-being-called/210066/2 "2011-07-29T16:35:37Z")

</div>

On Fri, Jul 29, 2011 at 9:12 AM, Gaurang S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> startup and shutdown method is not being called. And i don’t know why??  
> any idea guys ???

Modules don’t work that way. You’re defining startup and shutdown on  
BaseClass’ singleton class, which is not pointed to by DemoTest1’s  
singleton  
class. You should ether define those methods in another module and then  
extend DemoTest1, like this:

module BaseClassClassMethods  
def startup  
p “startup”  
end

def shutdown  
p “shutdown”  
end  
end

module BaseClassInstanceMethods  
def setup  
p “setup”  
end

def teardown  
p “teardown”  
end

def cleanup  
p “cleanup”  
end  
end

require ‘rubygems’  
gem ‘test-unit’  
require ‘test/unit’

class DemoTest1 \< Test::Unit::TestCase  
include BaseClassInstanceMethods  
extend BaseClassClassMethods

def test\_third()  
puts “third”  
end  
end

Or you could hook into the inclusion like this:

module BaseClass  
def self.included(klass)  
klass.instance\_eval do  
def startup  
p “startup”  
end

```
  def shutdown
    p "shutdown"
  end
end

```

end

def setup  
p “setup”  
end

def teardown  
p “teardown”  
end

def cleanup  
p “cleanup”  
end  
end

require ‘rubygems’  
gem ‘test-unit’  
require ‘test/unit’

class DemoTest1 \< Test::Unit::TestCase  
include BaseClass

def test\_third()  
puts “third”  
end  
end
