Project

General

Profile

Actions

Feature #3346

closed

__DIR__ revisted

Added by trans (Thomas Sawyer) over 14 years ago. Updated about 12 years ago.

Status:
Closed
Target version:
[ruby-core:30434]

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. #relative_require 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


Files

3346.pdf (41.5 KB) 3346.pdf 1-page presentation slide for the feature request meeting ([ruby-dev:45708]) yhara (Yutaka HARA), 06/30/2012 02:13 AM
__dir__.patch (1.38 KB) __dir__.patch authorNari (Narihiro Nakamura), 11/01/2012 11:02 PM

Related issues 4 (0 open4 closed)

Related to Ruby master - Bug #7729: __dir__ returns a absolute dir pathRejectedauthorNari (Narihiro Nakamura)01/23/2013Actions
Is duplicate of Ruby master - Feature #1961: Kernel#__dir__Closedmatz (Yukihiro Matsumoto)08/19/2009Actions
Has duplicate Ruby master - Bug #7975: Why __dir__, not __DIR__Rejected02/27/2013Actions
Has duplicate Ruby master - Feature #8098: Add __DIR__ to compliment __FILE__Rejected03/15/2013Actions
Actions #1

Updated by naruse (Yui NARUSE) over 14 years ago

=begin
I like DIR.
=end

Actions #2

Updated by shyouhei (Shyouhei Urabe) over 14 years ago

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

Actions #3

Updated by usa (Usaku NAKAMURA) over 14 years ago

=begin
Hello,

In message "[ruby-core:30436] [Feature #3346] DIR revisted"
on May.26,2010 20:59:48, wrote:

I like DIR.

I like Kernel#dir.

Regards,

U.Nakamura

=end

Actions #4

Updated by rkh (Konstantin Haase) over 14 years ago

=begin

I like Kernel#dir.

But then there should be Kernel#file.

Konstantin
=end

Actions #5

Updated by Eregon (Benoit Daloze) over 14 years ago

=begin
Hi,
On 26 May 2010 14:17, U.Nakamura wrote:

Hello,

In message "[ruby-core:30436] [Feature #3346] DIR revisted"
   on May.26,2010 20:59:48, wrote:

I like DIR.

I like Kernel#dir.

Regards,

U.Nakamura

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 "require_relative" 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:

  • require_relatives
  • 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

Actions #6

Updated by rogerdpack (Roger Pack) over 14 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

Actions #7

Updated by matz (Yukihiro Matsumoto) over 14 years ago

=begin
Hi,

In message "Re: [ruby-core:30434] [Feature #3346] DIR revisted"
on Wed, 26 May 2010 20:45:55 +0900, Thomas Sawyer 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

Actions #8

Updated by zenspider (Ryan Davis) over 14 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 DIR_DIR_DIR... I'd use them lots.

=end

Actions #9

Updated by trans (Thomas Sawyer) over 14 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.expand_path(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

Updated by mame (Yusuke Endoh) over 12 years ago

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

Updated by matz (Yukihiro Matsumoto) over 12 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.

Updated by trans (Thomas Sawyer) over 12 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 read_template(name)
file = File.dirname(FILE) + "/templates/#{name}.rb"
raise "no such template -- #{name}" unless File.exist?(file)
File.read(file)
end

Updated by yhara (Yutaka HARA) over 12 years ago

Adding presentation slide for the feature request meeting ([ruby-dev:45708])

Updated by mame (Yusuke Endoh) over 12 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

Updated by naruse (Yui NARUSE) over 12 years ago

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

Updated by rogerdpack (Roger Pack) over 12 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.

Updated by mame (Yusuke Endoh) over 12 years ago

  • Assignee changed from matz (Yukihiro Matsumoto) to nobu (Nobuyoshi Nakada)

Thomas Sawyer and Yutaka Hara,

Matz determined Kernel#dir (as a method).

Yhara-san, do you have a patch?

--
Yusuke Endoh

Actions #18

Updated by matz (Yukihiro Matsumoto) over 12 years ago

  • Status changed from Assigned to Closed

Updated by shyouhei (Shyouhei Urabe) over 12 years ago

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

This one is alive.

Updated by Eregon (Benoit Daloze) over 12 years 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.

Updated by rogerdpack (Roger Pack) over 12 years 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...

Updated by yhara (Yutaka HARA) about 12 years 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.

Updated by authorNari (Narihiro Nakamura) about 12 years 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?

Actions #24

Updated by authorNari (Narihiro Nakamura) about 12 years 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 (f_current_dirname): 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0