# How to redefine puts in .rbw files

**URL:** <https://www.ruby-forum.com/t/how-to-redefine-puts-in-rbw-files/92262>\
**Category:** Ruby\
**Created:** [April 17, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/how-to-redefine-puts-in-rbw-files/92262 "2007-04-17T17:20:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jano\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@Jano\_S](https://www.ruby-forum.com/u/Jano_S)\
**Post date:** [April 17, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/how-to-redefine-puts-in-rbw-files/92262/1 "2007-04-17T17:20:40Z")

</div>

(I’m ccing ruby-talk so that you might get more answers)

From  
[http://rubyforge.org/tracker/?func=detail&atid=715&aid=9018&group\_id=167](http://rubyforge.org/tracker/?func=detail&atid=715&aid=9018&group_id=167)  
Date: 2007-04-17 17:09  
Sender: M Jessick

What is the ruby-ish way to protect any stray “puts”  
calls leftover in a .rb fle that you want to run as .rbw?

For example, I develop in in a .rb file, then run it .rbw. If  
I leave in a puts call it throws. Rather than go through and  
comment them all out (or worse, wrap them all in begin rescue  
end blocks ;), I would prefer them to be ignored through some  
magic.

Is there a slick ruby way to do this? e.g.: overload puts and  
call the system puts within the overload within a begin rescue  
end.

For example:

begin  
puts “puts call that used to be ignored but now raises  
an exception in rubyw.exe since sometime in 2006”  
rescue SystemCallError  
end

would solve the problem, but if I was smart enough to do this  
everywhere, I would probably know how to do this most conveniently  
in ruby 😉

Try:

module Kernel  
alias\_method :orig\_puts, :puts  
def puts(\*args)  
orig\_puts \*args if $stdout  
end  
end

(not tested)

Jano

---

<div class="post-metadata">

**Author:** ![Jano\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@Jano\_S](https://www.ruby-forum.com/u/Jano_S)\
**Post date:** [April 17, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/how-to-redefine-puts-in-rbw-files/92262/2 "2007-04-17T17:47:46Z")

</div>

Hi,

At Wed, 18 Apr 2007 00:19:23 +0900,  
Jano S. wrote in [ruby-talk:248234]:

> Is there a slick ruby way to do this? e.g.: overload puts and  
> call the system puts within the overload within a begin rescue  
> end.

The global “puts” calls STDOUT.write.

def STDOUT.write(str)  
super  
rescue Errno::EBADF  
def self.write(str)  
end  
end
