Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Get filename without the extensions

Tags:

ruby

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".

like image 349
konyak Avatar asked Apr 29 '14 06:04

konyak


1 Answers

Read documentation:

basename(file_name [, suffix] ) → base_name

Returns the last component of the filename given in file_name, which can be formed using both File::SEPARATOR and File::ALT_SEPARATOR as the separator when File::ALT_SEPARATOR is not nil. If suffix is given and present at the end of file_name, it is removed.

=> File.basename('public/500.html', '.html') => "500" 

in you case:

=> File.basename("test.html.erb", ".html.erb") => "test" 
like image 178
Philidor Avatar answered Sep 17 '22 13:09

Philidor