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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:55:31+00:00 2026-06-15T00:55:31+00:00

I’m not sure if there’s something I did wrong here in some config files.

  • 0

I’m not sure if there’s something I did wrong here in some config files. When I run rackup -D, doing this:

File.expand_path(__FILE__)

actually returns /file.rb. And adding File.dirname somehow returns /. So now all my file loading code didn’t work as it tries to look in the / directory, rather than the project directory.

This does not happen if I remove the -D option. It returns the full path /home/blablabla/stuff/file.rb

Sample code:

test.rb:

require 'rubygems' if RUBY_VERSION <= '1.8.7'
require 'sinatra'

get '/expdir' do
  File.expand_path(File.dirname(__FILE__))
end

get '/exp' do
  File.expand_path(__FILE__)
end

get '/file' do
  __FILE__
end

get '/dirname' do
  File.dirname(__FILE__)
end

get '/dir' do
  Dir.entries(File.expand_path(File.dirname(__FILE__))).to_s
end

config.ru:

require 'test.rb'

run Sinatra::Application

Executed it with rackup -p 4567 and watched it return the correct values.
Executed it with rackup -p 4567 -D and watched it return the wrong values.

  • 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-15T00:55:32+00:00Added an answer on June 15, 2026 at 12:55 am

    Rack does indeed change the working directory to / when run as a daemon.

    In Ruby 1.8.7, __FILE__ in a required file refers to the path used to load the file, which could be a relative path from the current working directory of the process. However this value is not updated if the working directory is later changed, e.g. with a call to Dir.chdir.

    File.expand_path expands relative files paths relative to the working directory. So in this case File.expand_path(__FILE__) results in the paths relative to the root, but the value of __FILE__ is relative to the original working directory, giving the wrong result.

    In Ruby 1.9.2 and 1.9.3, __FILE__ in a required file refers to the absolute path of the file, so this problem doesn’t arise.

    One way to fix this in Ruby 1.8.7 is to use an absolute path when requiring your application file. Change the line require 'test.rb' in your config.ru to:

    require File.expand_path('../test', __FILE__)
    

    Now the references to __FILE__ will be absolute, so won’t be affected by the change in working directory when daemonising.

    If your application is more complicated, with more files, it might be better to set up your load path instead. For example your could put all your .rb files into a lib/ directory, then in your config.ru add:

    $LOAD_PATH.unshift(File.expand_path '../lib', __FILE__)
    

    You can then just require your files without worrying about the relative paths.

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

Sidebar

Related Questions

I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have just tried to save a simple *.rtf file with some websites and
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.