NamedResource complete

I've been working in my spare moments on designing and implementing the NamedResource class. It's now pretty much complete, and I'm quite happy with the result. In addition to using it for Music, Sound, and Surface, it's also a useful mixin for other things. Here's a snippet from the docs, to give you a taste of how nice it is:

Here's an example of how you could use this for a class which loads maps from a file:

class Map
  include Rubygame::NamedResource

  Map.autoload_dirs = [ File.join("maps","world_1"),
                        File.join("maps","custom") ]

  def autoload( filename )
    # Searches autoload_dirs for the file
    path = find_file( filename )

    if( path )
      return load_map( path )
      return nil

  def load_map( path )
    # Your code to do the real loading, then return
    # the created instance of Map class.
    # ...
    return map_instance

Here's an example of how you could then use the Map class:

map = Map[""]

if( map )
  start_playing( map )
  raise "Oops! The map file for Level 1 doesn't exist!"

NamedResource will be shipped in Rubygame 2.3. Now I just need to include it in Sound, Music, and Surface classes. Woohoo!


Have something interesting to say about this post? Email your thoughtful comments to