You can use [-4..-1]
or [-4, 4]
:
'this.is.really.dangerous.file.exe'[-4..-1]
# => ".exe"
'this.is.really.dangerous.file.exe'[-4, 4]
# => ".exe"
'a_file.rb'[-4..-1]
# => "e.rb"
But File::extname
is more appropriate to get filename.
File.extname 'a_file.cmd'
# => ".cmd"
File.extname 'a_file.rb'
# => ".rb"
File.extname 'this.is.really.dangerous.file.exe'
# => ".exe"