From: Robert Klemme Date: 2004-06-07T17:18:47+09:00 Subject: Re: Check assertion in Ruby ------=_NextPart_000_002D_01C44C78.6EA4B110 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit "Jean-Hugues ROBERT" schrieb im Newsbeitrag news:6.0.1.1.0.20040605205551.01e159b0@pop.mail.yahoo.com... > >Seriously, who needs CPP when there is Ruby? > > > >def load_special(file) > > if DEBUG > > load file > > else > > Object.new.instance_eval File.read(file).gsub(/^\s*assert\b/m, '#' ) > > end > >end > >Regards > > robert > > Cool ! > Now. What about multi-lines asserts ? Can't be done with the simple code above. > If that helps I may concede a end-of-xxx marker. Well, the proper way would be to write a parser for a macro language. You can get quite far with a simple hack like the one attached, although that does only support nested macro invocations without using () for sub macros. (Reason is that you can't parse nested brackets with regular expressions.) Have fun robert ------=_NextPart_000_002D_01C44C78.6EA4B110 Content-Type: application/octet-stream; name="mac-test.rb" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="mac-test.rb" #!/usr/bin/ruby require 'macro' MACRO.load "test.rbm" ------=_NextPart_000_002D_01C44C78.6EA4B110 Content-Type: application/octet-stream; name="test.rbm" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="test.rbm" #DEFMACRO foo(a,b) p a p b "\"#{a + b}\"" #ENDMACRO puts "foo" puts MACRO.foo( "x", "y" ) ------=_NextPart_000_002D_01C44C78.6EA4B110 Content-Type: application/octet-stream; name="Macro.rb" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Macro.rb" module MACRO def self.load(file) contents = File.read(file) buffer = nil raw = "" contents.each_line do |line| case line when /\A\s*#DEFMACRO\s+\b(.*)\Z/ buffer = "def self.#{$1}\n" when /\A\s*#ENDMACRO\s*\Z/ buffer << "end\n" # puts "DEFINING MACRO", buffer self.instance_eval buffer buffer = nil else if buffer buffer << line else raw << line end end end Object.new.instance_eval( raw.gsub( /MACRO.\w+\([^)]*\)/m ) do |m| instance_eval m end ) end end ------=_NextPart_000_002D_01C44C78.6EA4B110--