"Guilherme T." <zarawesome / gmail.com> schrieb im Newsbeitrag
news:57e813c9.0412170459.47d42421 / posting.google.com...
> I'd like to use rb_require(), but instead of a filename give it a
> string with the contents to be required. How would I go about doing
> this?

What you want to do is basically execute a piece of code once, right?  In
that case you could do something like this:

Cheap solution:

$code_snippets ||= Hash.new {|h,code| h[code] = eval( code ) }


# will print "buh!" only once
$code_snippets[ 'puts "buh"' ]
# ...
$code_snippets[ 'puts "buh"' ]


A bit nicer

module Code
  def self.snippets
    @code_snippets ||= Hash.new {|h,code| h[code] = eval( code ) }
  end

  def self.require( code )
    snippets[ code ]
  end
end

# will print "bah!" only once
Code.require 'puts "bah!"'
Code.require 'puts "bah!"'
Code.require 'puts "bah!"'
Code.require 'puts "bah!"'

Regards

    robert