4

using ruby 2.0.0p481 (2014-05-08) [x64-mingw32] on Win7

In a directory (d:\download\ruby to be exact) I have two files - calling.rb and called.rb

calling.rb

require_relative 'called' print_path() 

called.rb

def print_path() puts "File.expand_path(File.dirname(__FILE__)) is #{File.expand_path(File.dirname(__FILE__))}" puts "File.expand_path(File.dirname(File.dirname(__FILE__))) is #{File.expand_path(File.dirname(File.dirname(__FILE__)))}" puts File.expand_path(File.dirname(__FILE__)) end print_path() 

Could someone explain why when executing directly called.rb gives me

d:\download\ruby>ruby called.rb File.expand_path(File.dirname(__FILE__)) is d:/download/ruby File.expand_path(File.dirname(File.dirname(__FILE__))) is d:/download/ruby d:/download/ruby 

different result as when using the same code from calling.rb?

d:\download\ruby>ruby calling.rb File.expand_path(File.dirname(__FILE__)) is d:/download/ruby File.expand_path(File.dirname(File.dirname(__FILE__))) is d:/download d:/download/ruby File.expand_path(File.dirname(__FILE__)) is d:/download/ruby File.expand_path(File.dirname(File.dirname(__FILE__))) is d:/download d:/download/ruby d:\download\ruby> 
3
  • 1
    @vgoff, is see nothing wrong here, the only place Radek uses slashes is in his prompt and in his explanation where he shows the path where the files are run and on a windows machine that is correct Commented Aug 15, 2015 at 12:58
  • 2
    the output from Ruby puts _FILE_ are forward slashes in every OS, welcome to the windows world of headaches 8>) Commented Aug 15, 2015 at 13:12
  • The command line prompts are clearer now and separates the output a little more clearly. Commented Aug 15, 2015 at 14:39

2 Answers 2

4

Because __FILE__ is the current source file name, and 'current' is the keyword here.

You are also getting the Directory name of a directory in your nesting.

To troubleshoot this, perhaps the following puts lines will help:

def print_path() puts "File.expand_path(File.dirname(__FILE__)) is #{File.expand_path(File.dirname(__FILE__))}" puts "File.expand_path(File.dirname(File.dirname(__FILE__))) is #{File.expand_path(File.dirname(File.dirname(__FILE__)))}" puts File.expand_path(File.dirname(__FILE__)) end 
Sign up to request clarification or add additional context in comments.

8 Comments

To trace when the __FILE__ value changes, you can place some puts lines in both files. You will then see what the current file is, and how it is represented by the __FILE__ variable.
could you suggest where and what to use to debug? I'd say that the puts I used is good enough to see what is happening.
My example has the non-nested as the last line, but shows both ways in the troubleshooting puts lines above it.
got it. I copied the code from somewhere and either nested it by mistake or it was like that. Thank you for the explanation.
But the nesting doesn't explain why, when executing the called.rb directly I get the same path in all 3 lines. - I updated my question. I used your debugging lines and added the results from that.
|
0

You have done it everything perfectly and the results are also correct. The only noticeable thing here is,

  1. When you execute called.rb, the print_path() executed only one time
  2. But when you execute calling.rb, the print_path() executed twice. Because you required the whole file called.rb in calling.rb. So first, called.rb file's print_path() method is executed and then the calling.rb file's print_path() method executed again.

That's why you received the same result twice. Regarding the same result case, you placed both files in same directory. That's why your results also same in both print_path() execution.

You will get the different results if you place anyone of the above file under different directory.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.