Ruby Notes

Here are some notes on Ruby that may come in handy later.

Class Inheritance

If you have subclasses that call functions from a superclass, you can figure out which subclass called the super's function simply by using self.class, like so: class SuperClass def test puts "I was called by #{self.class.to_s}!" end end

module SomeModule class SubClassOne < SuperClass end

class SubClassTwo < SuperClass end end

SomeModule::SubClassOne.new.test # I was called by SomeModule::SubClassOne! SomeModule::SubClassTwo.new.test # I was called by SomeModule::SubClassTwo!

IO redirection

Here's something pretty nifty I learned today with help from folks in the #ruby-lang channel. To redirect stderr to something else, do this: rd, wr = IO.pipe $stderr.reopen(wr) $stderr.puts "test" $stderr.puts "junk" wr.close $stderr.close rd.read

You can save the original $stderr by using dup.
Topic revision: r2 - 09 Aug 2006, JeremyStephens
 

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback