Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

File Extension Methods

DZone's Guide to

File Extension Methods

·
Free Resource
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"
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}