From: Trans Date: 2007-01-14T11:15:20+09:00 Subject: Re: return_on Daniel Finnie wrote: > I have made the most hackish thing to accomplish this. It is basically > a regexp-supporting preprocessor. I don't recommend using it. > > #! /usr/bin/ruby -w > > def preprocessor_define(*args, &blk) > # Determine line of caller > callerLine = caller.join.match(/[0-9]+/)[0].to_i > > # Get the source of the running file > source = IO.read(__FILE__) > > # Remove the callerLine line > source = source.split("\n") > source.delete_at(callerLine - 1) > source = source.join("\n") > > # Do the replacement > source.gsub!(*args, &blk) > > # Avoid function redefinition warnings > newName = "" > 50.times {newName << (rand(26) + 65).chr } > source.gsub!("preprocessor_define", newName) > > # Run the replacement > #puts source > eval source > exit > end > > preprocessor_define(/return_on (.*)/) {|s| "return #{s[1]} unless > #{s[1]}.nil?"} > > def test > zulu = 5 > return_on zulu > puts "Broken." > end > test > > Output: None, as 'puts Broken' never gets executed. Holy Smokes! The Man goes out of his way!!! return_on brother, return_on! :-) T.