Project

General

Profile

Bug #13126

__dir__ in eval returns nil

Added by metadeus (Artem Shein) over 2 years ago. Updated 24 days ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
[ruby-core:79067]

Description

$ ruby -e 'eval "p File.dirname(__FILE__); p __dir__"'
"."
nil

History

Updated by shyouhei (Shyouhei Urabe) over 2 years ago

  • Status changed from Open to Feedback

Can you show us your expectation? Should what be the value for __dir__ inside of an eval?

Updated by metadeus (Artem Shein) over 2 years ago

Shyouhei Urabe wrote:

Can you show us your expectation? Should what be the value for __dir__ inside of an eval?

It should be the same as File.dirname(__FILE__) or at least File.dirname(File.realpath(__FILE__)) (like your documentation say).

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

Artem Shein wrote:

It should be the same as File.dirname(__FILE__) or at least File.dirname(File.realpath(__FILE__)) (like your documentation say).

When the third argument is not given, __FILE__ in eval is "(eval)".
Do you mean that __dir__ should be "."?

Updated by metadeus (Artem Shein) over 2 years ago

Nobuyoshi Nakada wrote:

When the third argument is not given, __FILE__ in eval is "(eval)".
Do you mean that __dir__ should be "."?

No, I mean that documentation is saying:
"The return value equals to File.dirname(File.realpath(__FILE__))." here: http://ruby-doc.org/core-2.3.0/Kernel.html#method-i-__dir__

I've replaced the "File.dirname(File.realpath(__FILE__))" in my source code with "__dir__" and got different result. I don't care about the exact value, but I expect it to be the same.

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

Artem Shein wrote:

I've replaced the "File.dirname(File.realpath(__FILE__))" in my source code with "__dir__" and got different result. I don't care about the exact value, but I expect it to be the same.

The former also does not work in eval as expected unless a real path name is given.
Please elaborate on the concrete code.

#6

Updated by jeremyevans0 (Jeremy Evans) 24 days ago

  • Status changed from Feedback to Closed

Also available in: Atom PDF