w3resource

Ruby Basic Exercises: Print the extension name of the file

Ruby Basic: Exercise-7 with Solution

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

Ruby Code Editor:

Contribute your code and comments through Disqus.

p>Previous: 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.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.