Skip to content

Instantly share code, notes, and snippets.

@fairchild
Created December 23, 2008 06:33
Show Gist options
  • Select an option

  • Save fairchild/39271 to your computer and use it in GitHub Desktop.

Select an option

Save fairchild/39271 to your computer and use it in GitHub Desktop.
A nice example of Meta programing from the earworm project
{
'ogg' => Audio::OGG::Decoder,
'mp3' => Audio::MPEG::Decoder,
}.each do |type,klass|
define_method(:"decode_#{type}") do |filename|
reader = klass.new
name = File.join(Dir::tmpdir,
"#{File.basename(filename, ".#{type}")}.wav")
File.open(filename, 'rb') { |input|
File.open(name, 'wb') { |tmpfile|
reader.decode(input, tmpfile)
}
}
name
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment