Feature #3346

__DIR__ revisted

Added by Thomas Sawyer almost 4 years ago. Updated over 1 year ago.

[ruby-core:30434]
Status:Closed
Priority:Normal
Assignee:Yutaka HARA
Category:core
Target version:2.0.0

Description

=begin
I'd like to know why DIR was rejected? I use File.dirname(FILE) all the time, and I frequently see others do so as well. #relativerequire is helpful but it covers only one specific use case --and probably not the most common one. I am often using File.dirname(FILE) in build scripts, when I am loading examples for tests, and when I load output templates or other pluggable modules that reside relative to my code. For something so common, having to clutter my code with a 22 character sequence, when an perfectly obvious 7 character sequence would do semms very uncharacteristic of Ruby, which is usually quite concise. Indeed, it is not uncommon to see code that defines a constant such as DIR = File.dirname(FILE_) when it will be used more than once because it quickly becomes an eye-sore. For these reasons I hope you will reconsider the earlier rejection.
=end

3346.pdf - 1-page presentation slide for the feature request meeting ([ruby-dev:45708]) (41.5 KB) Yutaka HARA, 06/30/2012 02:13 AM

__dir__.patch Magnifier (1.38 KB) Narihiro Nakamura, 11/01/2012 11:02 PM


Related issues

Related to ruby-trunk - Bug #7729: __dir__ returns a absolute dir path Rejected 01/23/2013
Duplicates ruby-trunk - Feature #1961: Kernel#__dir__ Closed 08/19/2009
Duplicated by ruby-trunk - Bug #7975: Why __dir__, not __DIR__ Rejected 02/27/2013
Duplicated by ruby-trunk - Feature #8098: Add __DIR__ to compliment __FILE__ Rejected 03/15/2013

Associated revisions

Revision 37432
Added by nari over 1 year ago

  • eval.c (fcurrentdirname): add the new method for Kernel.
    This method almotst same as File.dirname(FILE). One
    different behavior is it returns nil when FILE returns nil.
    [Feature #3346]

  • NEWS: ditto

  • test/ruby/test_method.rb: related test.

History

#1 Updated by Yui NARUSE almost 4 years ago

=begin
I like DIR.
=end

#2 Updated by Shyouhei Urabe almost 4 years ago

=begin
Which is "the earlier rejection"? I see nothing (though this request is not accepted yet).
=end

#3 Updated by Usaku NAKAMURA almost 4 years ago

=begin
Hello,

In message " [Feature #3346] DIR revisted"
on May.26,2010 20:59:48, redmine@ruby-lang.org wrote:

I like DIR.

I like Kernel#dir.

Regards,
--
U.Nakamura usa@garbagecollect.jp

=end

#4 Updated by Konstantin Haase almost 4 years ago

=begin

I like Kernel#dir.

But then there should be Kernel#file.

Konstantin
=end

#5 Updated by Benoit Daloze almost 4 years ago

=begin
Hi,
On 26 May 2010 14:17, U.Nakamura usa@garbagecollect.jp wrote:

Hello,

In message " [Feature #3346] DIR revisted"
   on May.26,2010 20:59:48, redmine@ruby-lang.org wrote:

I like DIR.

I like Kernel#dir.

Regards,

U.Nakamura usa@garbagecollect.jp

I prefer DIR.

( as a keyword,
because it's clearly the same family with FILE,
and it's a constant (so in uppercase) )

My first use would be things like Dir[File.join(DIR,'.rb')].each
{ |f| require f }
That's only because I am sometimes too lazy to "requirerelative" all
the files under the same dir.
But maybe that's not the best way to do it, tough.
Currently, there is also another possibility, which is kind of a hack:
Dir[File.expand
path("../
.rb",FILE)].each { |f| require f }

Just some ideas in the air for this case:
- requirerelatives
- require
relative '.rb'
- require_relative '
' (then assume we want only .rb files)
- require_relative (then the meaning would be "require the relative
stuff, what is relative to this")
The 2nd and 3rd one obviously more flexible as they accept a glob
matching pattern.

Anyway, there are still many cases in which DIR would be useful,
even if that's kind of low-level stuff.

Regards,
B.D.

=end

#6 Updated by Roger Pack almost 4 years ago

=begin

Which is "the earlier rejection"? I see nothing (though this request is not accepted yet).

#1961 wasn't rejected, just not accepted/committed yet.

For me, the "ideal" would be a compiler time constant FILE, but if it's a method instead, then name it file just to not confuse people.

-rp
=end

#7 Updated by Yukihiro Matsumoto almost 4 years ago

=begin
Hi,

In message "Re: [Feature #3346] DIR revisted"
on Wed, 26 May 2010 20:45:55 +0900, Thomas Sawyer redmine@ruby-lang.org writes:

|I'd like to know why DIR was rejected?

I haven't rejected. I agreed with the basic idea, and proposed to add
Kernel#dir; a method not to add a new keyword, a lowercase name to
clarify it's implemented by a method. But some didn't agree with me,
so I haven't added.

                        matz.

=end

#8 Updated by Ryan Davis almost 4 years ago

=begin

On May 26, 2010, at 09:11 , Roger Pack wrote:

#1961 wasn't rejected, just not accepted/committed yet.

wow... it is just like Seattle politics... if we just keep voting OVER and OVER on the same issue, eventually it'll get accepted!

You should run for Seattle City Council.

Don't forget DIR_DIR and DIRDIRDIR... I'd use them lots.

=end

#9 Updated by Thomas Sawyer over 3 years ago

=begin
Good to hear. dir works for me if that is deemed better, though I concur that it inclines me to expect FILE to be file too. Also, I do not recall who suggested it, but an optional join parameter was once suggested too, e.g.

dir('foo')

Which would be equivalent to:

File.expandpath(File.join(File.dirname(FILE_),'foo'))

Such a parameter would seem more appropriate to a lowercase method name. Also perhaps it would quell the "DIR_DIR" gripes, which I don't earnestly get, but if they should be taken seriously...

dir('..')

=end

#10 Updated by Yusuke Endoh about 2 years ago

  • Description updated (diff)
  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

#11 Updated by Yukihiro Matsumoto about 2 years ago

UNIX style directory hierarchy prepare a directory to put everything relate to the app/lib. I hesitate to add a method/directive that encourage file system convention apparently against UNIX style.

#12 Updated by Thomas Sawyer about 2 years ago

Ruby does not provide way to get root of app/lib, and current working directory is not always root of applicable app/lib, so that's not always helpful way.

Sometimes practical choice outweighs theoretical ideal. File.dirname(__FILE__) is exhaustively used idiom.

Consider another use case:

myapp/
lib/
myapp/
template.rb
templates/
foo.erb
bar.erb

In template.rb there will be something like:

def readtemplate(name)
file = File.dirname(
FILE_) + "/templates/#{name}.rb"
raise "no such template -- #{name}" unless File.exist?(file)
File.read(file)
end

#13 Updated by Yutaka HARA almost 2 years ago

Adding presentation slide for the feature request meeting ()

#14 Updated by Yusuke Endoh almost 2 years ago

Yhara-san, your slide is received. Thank you!

But, matz has already accepted this proposal basically.
The remaining issue is its API (including name). Right?
If so, I'm afraid if just listing candidates will make no progress.
It may be good to choose one target yourself and make it authorized by matz.

Anyway, your slide will be presented at the meeting.
Matz may choose one in his preference. Thanks again!

Yusuke Endoh mame@tsg.ne.jp

#15 Updated by Yui NARUSE almost 2 years ago

I was against the name DIR for years,
but now I'm for dir if file and line method is also added.

#16 Updated by Roger Pack almost 2 years ago

I'm for dir or DIR since I use File.dirname(FILE) a lot. Barely prefer DIR since FILE already exists so it keeps parity with it.

#17 Updated by Yusuke Endoh over 1 year ago

  • Assignee changed from Yukihiro Matsumoto to Nobuyoshi Nakada

Thomas Sawyer and Yutaka Hara,

Matz determined Kernel#dir (as a method).

Yhara-san, do you have a patch?

Yusuke Endoh mame@tsg.ne.jp

#18 Updated by Yukihiro Matsumoto over 1 year ago

  • Status changed from Assigned to Closed

#19 Updated by Shyouhei Urabe over 1 year ago

  • Category set to core
  • Status changed from Closed to Assigned
  • Assignee changed from Nobuyoshi Nakada to Yutaka HARA
  • Target version set to 2.0.0

This one is alive.

#20 Updated by Benoit Daloze over 1 year ago

mame (Yusuke Endoh) wrote:

Thomas Sawyer and Yutaka Hara,

Matz determined Kernel#dir (as a method).

Yhara-san, do you have a patch?

Will dir always be an absolute path, or just File.dirname(FILE)?

File.dirname(FILE) is relative (often ".") when FILE is $0.
It would mean the path needs to be expanded if it is relative (using File.expand_path), to keep the right location if the current working directory changes.

#21 Updated by Roger Pack over 1 year ago

definitely recommend full path to avoid possible confusion, like we used to have with 1.8.x not using the full path for $: but that's just my opinion...

#22 Updated by Yutaka HARA over 1 year ago

mame (Yusuke Endoh) wrote:

Thomas Sawyer and Yutaka Hara,

Matz determined Kernel#dir (as a method).

Yhara-san, do you have a patch?

Sorry, I forgot to reply. I don't have a patch right now.

#23 Updated by Narihiro Nakamura over 1 year ago

Hi.

yhara (Yutaka HARA) wrote:

mame (Yusuke Endoh) wrote:

Thomas Sawyer and Yutaka Hara,

Matz determined Kernel#dir (as a method).

Yhara-san, do you have a patch?

Sorry, I forgot to reply. I don't have a patch right now.

I've created a patch for Kernel#dir. How about it?

#24 Updated by Narihiro Nakamura over 1 year ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r37432.
Thomas, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • eval.c (fcurrentdirname): add the new method for Kernel.
    This method almotst same as File.dirname(FILE). One
    different behavior is it returns nil when FILE returns nil.
    [Feature #3346]

  • NEWS: ditto

  • test/ruby/test_method.rb: related test.

Also available in: Atom PDF