Never been to DZone Snippets before?

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

file extension methods (See related posts)

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 on this post

c00lryguy posts on May 17, 2008 at 18:54
Why not:
    def method_missing(meth, *args)
      [self, '.', meth.to_s].join ? Extentions =~ meth.to_s : super
    end


Yanno.. to make it purdy?
c00lryguy posts on May 17, 2008 at 18:58
>_> <_< I ment
def method_missing(meth, *args)
  Extensions =~ meth.to_s ? [self, '.', meth.to_s].join : super
end

You need to create an account or log in to post comments to this site.


Click here to browse all 5144 code snippets

Related Posts