DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

File Extension Methods

05.16.2008
| 3921 views |
  • submit to reddit
        Allows you to use file extensions as methods

class File
  # Feel free to add more here, as you need them.
  Extensions = %r=^(txt|rb|markdown|textile|haml|sass|css|html|xhtml)$=i
  
  module Extension
    def method_missing(meth, *args)
      if Extensions =~ meth.to_s
        [self, '.', meth.to_s].join
      else
        super
      end # if
    end # method_missing
  end # Extension
end # File

class Symbol
  include File::Extension
end
class String
  include File::Extension
end

'myfile'.html
# => "myfile.html"

:a_file.rb
# => "a_file.rb"
    

Comments

Ryan Lewis replied on Sat, 2008/05/03 - 2:54pm

>_> <_< I ment def method_missing(meth, *args) Extensions =~ meth.to_s ? [self, '.', meth.to_s].join : super end

Ryan Lewis replied on Sat, 2008/05/03 - 2:54pm

Why not: def method_missing(meth, *args) [self, '.', meth.to_s].join ? Extentions =~ meth.to_s : super end Yanno.. to make it purdy?