[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

temporarily redefine the 'puts' command?

aidy

8/22/2008 2:57:00 PM

Hi,

Is it possible to temporarily redefine the 'puts' command?

I have a gem that unnecessarily writes to the console, which effects
the Rspec Story Runner logging,

I do not want to manual comment out the puts as I use this gem on
numerous machines

Aidy
4 Answers

Phill Davies

8/22/2008 3:15:00 PM

0

Sure you can, that's one of the beautiful things about Ruby.

module Kernel
def puts (s)
# Do whatever you want here, including nothing
end
end



aidy wrote:
> Hi,
>
> Is it possible to temporarily redefine the 'puts' command?
>
> I have a gem that unnecessarily writes to the console, which effects
> the Rspec Story Runner logging,
>
> I do not want to manual comment out the puts as I use this gem on
> numerous machines
>
> Aidy

--
Posted via http://www.ruby-....

Daniel Lucraft

8/22/2008 3:16:00 PM

0

aidy wrote:
> Hi,
>
> Is it possible to temporarily redefine the 'puts' command?
>
> I have a gem that unnecessarily writes to the console, which effects
> the Rspec Story Runner logging,
>
> I do not want to manual comment out the puts as I use this gem on
> numerous machines
>
> Aidy


$ irb
irb(main):001:0> alias :oldputs :puts
=> nil
irb(main):002:0> def puts(*args); end
=> nil
irb(main):003:0> puts "hi"
=> nil
irb(main):004:0> alias :puts :oldputs
=> nil
irb(main):005:0> puts "hi"
hi
=> nil

howzat?

You probably want to explicitly do it in Kernel when you do it for real.
Something like this, if you need to do it dynamically.
eval %Q{
module Kernel
alias :oldputs :puts
def puts(*args)
end
end
}

best,
Dan
--
Posted via http://www.ruby-....

Eric I.

8/22/2008 3:58:00 PM

0

On Aug 22, 10:57 am, aidy <aidy.le...@googlemail.com> wrote:
> Is it possible to temporarily redefine the 'puts' command?
>
> I have a gem that unnecessarily writes to the console, which effects
> the Rspec Story Runner logging,
>
> I do not want to manual comment out the puts as I use this gem on
> numerous machines

Hi Aidy,

Here's some code, which should work for you. Of course you'll have to
answer the questions as where to put the methods puts_on and puts_off
(or whatever you decide to name them).

====

# replaces Kernel#puts w/ do-nothing method
def puts_off
Kernel.module_eval %q{
def puts(*args)
end
}
end

# restores Kernel#puts to its original definition
def puts_on
Kernel.module_eval %q{
def puts(*args)
$stdout.puts(*args)
end
}
end

puts "You can see me."
puts_off
puts "But I'm hidden."
puts_on
puts "But I'm viewable once again."

====

Eric

====

Interested in hands-on, on-site Ruby or Ruby on Rails
training? See http://Lea... for information about a
well-reviewed courses.

Matthias Reitinger

8/22/2008 8:13:00 PM

0

aidy wrote:
> Is it possible to temporarily redefine the 'puts' command?

I would suggest to reassign $stdout instead. This ensures that print,
putc et cetera won't output anything either.

---
class DevNull; def write(*args) end end

$stdout = DevNull.new
puts "This output will be discarded."
$stout = STDOUT
puts "And this one won't!"
---

You could even wrap this up in a nice helper method:

---
class DevNull; def write(*args) end end

module Kernel
def silent
$stdout = DevNull.new
yield
$stdout = STDOUT
end
end

silent do
puts "Nothing to see here..."
print "... and there!"
end
---

Regards,
Matthias
--
Posted via http://www.ruby-....