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 7844159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:47:28+00:00 2026-06-02T16:47:28+00:00

i have this simple script to show all files in a folder, it works

  • 0

i have this simple script to show all files in a folder, it works in the console but gives a different result in Sinatra (with path and extension). Why is this so, and how can i best present these basenames (without path and extension) in a ul list as a link to open this file in the browser using Sinatra ?
The goal is to present a clickable list of pages to open if no filename is given. I allready have the routine to show the files.

console:

require 'find'

def get_files path
 dir_array = Array.new
 Find.find(path) do |f|
    dir_array << f if !File.directory?(f) # add only non-directories  
 end
 return dir_array
end  

for filename in get_files 'c:/sinatra_wiki/views'
  basename = File.basename(filename, ".*")
  puts basename
end

=> index
   index2

Sinatra:

require 'find'

def get_files path
 dir_array = Array.new
 Find.find(path) do |f|
    dir_array << f if !File.directory?(f) # add only non-directories  
 end
 return dir_array
end  

get '/' do
  for filename in get_files 'c:/sinatra_wiki/views'
    basename = File.basename(filename, ".*")
    puts basename
  end
end
=> c:/sinatra_wiki/views/index.htmlc:/sinatra_wiki/views/index2.erb
  • 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-06-02T16:47:29+00:00Added an answer on June 2, 2026 at 4:47 pm

    In your sinatra implementation, the result you see in the browser is not the one from the puts basename statement in the get block. It’s the return value of the get_files method. Try adding puts "<p>#{base name}</p>" instead of the puts basename in the get block and see for yourself.

    Some changes:

    The get_files method: Instead of sending the entire file path, send only the file name

    dir_array << File.basename(f, ".*")
    

    Add a view in case you need clarity:

    get '/' do
       @arr = get_files(the_path)
       erb :index
    end
    

    elsewhere, in the app/views folder, in an index.erb file:

    <h2>Page list</h2>
    
    <ul> 
      <% @arr.each do |page| %>
        <li><a href="/<%=page%>"><%=page> %></a></li>
      <% end %>
    </ul> 
    

    This is to list out the file names in a similar way to that of the console output.

    TL;DR: Put the looping part in the view!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple php script <?php echo '<pre>'; // Outputs all the result
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have some questions about the performance of this simple python script: import sys,
This is a simple script I have written to test command line argument handling:
I have a simple script which is used to start another program. This other
I have a simple script that does some search and replace. This is basically
A really simple shell script question. I have a file with something like this:
I have a simple script that will send out a bunch of emails but
I have two files. location.php, that outputs this: [[javascript],[PHP]] and in another file: <script

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.