Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to redefine puts in .rbw files

Reply
Thread Tools

How to redefine puts in .rbw files

 
 
Jano Svitok
Guest
Posts: n/a
 
      04-17-2007
(I'm ccing ruby-talk so that you might get more answers)

From http://rubyforge.org/tracker/?func=d...8&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 rig_puts, uts
def puts(*args)
orig_puts *args if $stdout
end
end

(not tested)

Jano

 
Reply With Quote
 
 
 
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      04-17-2007
Hi,

At Wed, 18 Apr 2007 00:19:23 +0900,
Jano Svitok 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

--
Nobu Nakada

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Windows 7 puts a lock icon on new files. John Windows 64bit 0 06-25-2010 01:49 PM
temporarily redefine the 'puts' command? aidy Ruby 4 08-22-2008 08:12 PM
what's the diff between puts y and puts "#{y}" in class_eval Raj Singh Ruby 4 01-29-2008 10:16 PM
Suggestion: swap name of "puts" and "print" and rename "puts" to"put_s" Michael Brooks Ruby 22 03-27-2007 04:57 PM



Advertisments