File.without_extname

class << File
  def without_extname(filename)
    filename.gsub(/#{extname(filename)}$/, '')
  end
end

I’d love a better name for this method. Any ideas?

Demo

File.without_extname("test.rb")       # => "test" 
File.without_extname("a/b/d/test.rb") # => "a/b/d/test" 
File.without_extname("test")          # => "test" 
File.without_extname(".profile")      # => ".profile" 

What about Pathname?

Here you go:

class Pathname
  def without_extname
    Pathname(File.without_extname(@path))
  end
end

Extra! Extra!

The easiest way to go from foo/bar.baz to bar is:

File.basename(path, ".*")

(Yes, this is undocumented.)