Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6704019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:14:25+00:00 2026-05-26T07:14:25+00:00

I am trying to pass in the file extension as a variable but it

  • 0

I am trying to pass in the file extension as a variable but it doesn’t seem to be working. when I run getlist.rb(txt)

def getlist(extension)
  file = File.new("the_list.txt", "w")
  Dir['../path/*.'+ extension].each { |f| file.puts File.basename(f, '.'+ extension).upcase }
end

Basically I want to drop in any file extension (txt, pdf, rb, etc) and it will give me the list without the extension names. Script works fine when it is hard coded just doesn’t work when I try to drop in a variable.

Best Regards,

AZCards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:14:26+00:00Added an answer on May 26, 2026 at 7:14 am

    My example:

    def getlist(extension)
      File.open("the_list.txt", "w"){|file|
        Dir["*.#{extension}"].each { |f| 
            file << File.basename(f, File.extname(f)).upcase
            file << "\n"
        }
      }
    end
    
    getlist('rb')
    getlist(:rb)
    

    My main modifications:

    • File.open isnted new (you forgot file.close). With File.open and a block the close is done automatic.
    • Replaces String+ with #-replacements. Advantage: you can use symbols when you call it (you wrote getlist.rb(txt) – this only works, when txt is a variable, but I think you want ‘txt’)
    • Use File#extension., see Daves answer.
    • I added newlines to the resulting file.

    Update
    Just in case you need a solution with multiple extensions and to call it from command line:

    def getlist(*extensions)
      p "*.{#{extensions.join(',')}}"
      File.open("the_list.txt", "w"){|file|
        Dir["*.{#{extensions.join(',')}}"].each { |f| 
            file << File.basename(f, File.extname(f)).upcase
            file << "\n"
        }
      }
    end
    
    #getlist('rb', 'txt')
    #getlist(:rb, :txt)
    getlist(*ARGV)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass a variable from one include file to another. This is
I'm trying to pass variable from PHP file to Perl file by this code
I am trying to pass a variable to a vendor file like so: echo
I'm new in AS 3.0. I'm trying to pass flash variables to SWF file
im trying to pass on the id attribute to the file.php, but its giving
I am trying to pass a variable to a included file. I have done
I am trying to pass in arguments to the a parent file that is
I have been trying to debug the below python cgi code but doesn't seems
I'm trying to pass a form field of type file to a CFFUNCTION. The
When I pass File file to a method I'm trying to get its full

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.