Sha256: 3f9683157dd93b3d708cb8b3ddaa231d45bfe82686f3d0f31b336b5083c7aa8f

Contents?: true

Size: 783 Bytes

Versions: 1

Compression:

Stored size: 783 Bytes

Contents

# = Macros
#
# A powerfull macro implementation for Ruby. Allows definition
# of new macro.
#
# code: gmosx
#
# (c) 2004 Navel, all rights reserved.
# $Id: macros.rb 71 2004-10-18 10:50:22Z gmosx $

$__macros__ = {}
$__required__ = {}

module Kernel

alias_method :old_require, :require
def require(path)
	return if $__required__[path]
	
	source = File.read(path)
	
	# parse macro
	source.gsub!(/defmacro\s*\/(.*?)\/\s(.*?)endmacro/m) {
		$__macros__[Regexp.new($1)] = $2 ; ""
	}
	
	# expand macros	
	$__macros__.each { |match, replace|
		source.gsub!(match, replace)
	}
	
	$__required__[path] = true

	eval(source)
end

end

require "n/test1.rb"
require "n/test2.rb"

__END__

Examples:

defmacro /my_macro\((.*)\)/
	begin 
		my_function(\1)
	rescue => ex
		puts ex
	end 
endmacro

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.1.2 lib/n/macros.rb