ruby

Ruby: Get filename without the extensions


How can I get the filename without the extensions? For example, input of "/dir1/dir2/test.html.erb" should return "test".

In actual code I will passing in __FILE__ instead of "/dir1/dir2/test.html.erb".


Solution

  • Thanks to @xdazz and @Monk_Code for their ideas. In case others are looking, the final code I'm using is:

    File.basename(__FILE__, ".*").split('.')[0]
    

    This generically allows you to remove the full path in the front and the extensions in the back of the file, giving only the name of the file without any dots or slashes.