Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby filename from path

path = "/path/to/file.ext"

File.basename(path) # 'file.ext'
Comment

ruby file name path

path = "/path/to/xyz.mp4"

File.basename(path)         # => "xyz.mp4"
File.extname(path)          # => ".mp4"
File.basename(path, ".mp4") # => "xyz" OR
File.basename(path, ".*")   # => "xyz"
File.dirname(path)          # => "/path/to"

# Alternative to File.extname(path) for multiple extension periods
def full_extname(path)
  ext = File.basename(path).split('.', 2)[1]
  ext.nil? ? '' : ".#{ext}"
end

full_extname('/path/to/xyz.mp4')				# => ".mp4"
full_extname('/path/to/xyz.my.custom.ext.mp4')	# => ".my.custom.ext.mp4"
full_extname('/path/to/xyz')					# => ""
Comment

PREVIOUS NEXT
Code Example
Ruby :: ruby get current datetime utc 
Ruby :: ruby select first n elements from array 
Ruby :: check current route rails 
Ruby :: ruby refinement import dynamic methods 
Ruby :: exit program ruby 
Ruby :: rspec check if object of a class 
Ruby :: travis ci NameError: uninitialized constant SimpleCov 
Ruby :: ruby replace certain character 
Ruby :: ruby file write 
Ruby :: ruby/rails file get line number 
Ruby :: ruby multiline comment 
Ruby :: ruby catch all exceptions 
Ruby :: rails find_by order 
Ruby :: ruby check if value in array 
Ruby :: autoload lib directory rails 
Ruby :: ruby test is number 
Ruby :: ruby generate array of alphabet 
Ruby :: call a class method ruby 
Ruby :: ruby for loop 
Ruby :: rails helper in controller 
Ruby :: ruby rails find data field type 
Ruby :: grails 3 cron jobs 
Ruby :: how to differentiate get and post when it has same name in rails 
Ruby :: ruby constructors 
Ruby :: ruby remove nil element in array 
Ruby :: rails humanize date 
Ruby :: rails scope where not 
Ruby :: generate float array in ruby 
Ruby :: expect method call inside rescue rspec 
Ruby :: ruby md5 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =