Sha256: 82e6072d1f41f3aec3bec19181aad5cec3a76d6fde211e997b6024020190bb42

Contents?: true

Size: 534 Bytes

Versions: 1

Compression:

Stored size: 534 Bytes

Contents

require 'erb'

class ERB
	alias original_result result

	# add locals support
	#
	# @example
	#  erb = Erb.new("<%=a%>")
	#  erb.result(nil, a: 1) #=> "1"
	#
	def result bind=nil, locals={}
		bind ||= TOPLEVEL_BINDING
		if locals.empty?
			original_result bind
		else
			result_with_locals bind, locals
		end
	end

	private
	def result_with_locals bind, locals
		@locals = locals
		evalstr = <<-EOF
def run_erb
	#{locals.map{|k,v| "#{k} = @locals[:#{k}]"}.join(';')}
	#{self.src}
	_erbout
end
		EOF
		eval evalstr
		run_erb
	end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tagen-0.2.3 lib/tagen/erb.rb