w3resource

Ruby Basic Exercises: Print the extension name of the file


Write a Ruby program to accept a filename from the user print the extension of that.

Ruby Code:

file = "/user/system/test.rb"
# file name
fbname = File.basename file  
puts "File name: "+fbname 
# basename
bname = File.basename file,".rb"
puts "Base name: "+bname 
# file extention
ffextn = File.extname  file   
puts "Extention: "+ffextn 
# path name
path_name= File.dirname  file 
puts "Path name: "+path_name

Output:

File name: test.rb
Base name: test
Extention: .rb
Path name: /user/system

Flowchart:

Flowchart: Print the extension name of the file

Go to:


PREV : Write a Ruby program which accept the user's first and last name and print them in reverse order with a space between them.
NEXT : Write a Ruby program to check two integer values whether either of them is in the range 20..30 inclusive.

Ruby Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.