3

The docs of File.exist? says:

Return true if the named file exists.

Note that last word used; "exists". This is correct. "File exist" without the ending s is not correct.

The method File.exists? exists, but they deprecated this method. I am thinking it should have been the other way around. What am I missing?

Also, it's noteworthy that other languages/libraries use exists, for example Java and .NET.

Similarly, "this equals that" - but Ruby uses equal, again dropping the ending s. I am getting a feeling that Ruby is actively walking in another direction than mainstream. But then there has to be a reason?

Martin Andersson
  • 18,072
  • 9
  • 87
  • 115

3 Answers3

3

This is largely a subjective call. Do you read the call as "Does this file exist?" or "File exists"? Both readings have their merits.

Historically Ruby has had a lot of aliased methods like size vs. length, but lately it seems like the core team is trying to focus on singular, consistent conventions that apply more broadly.

You'd have to look closely at the conversations on the internal mailing list surrounding the decisions here. I can't find them easily, only people dealing with the changes as deprecation warnings pop up.

The Ruby core team is a mix of people who speak different languages but the native language is Japanese, so perhaps that's guiding some of these decisions. It could be a preference to avoid odd inflections on verbs.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • `size` and `length` do different things AFAIK. One of them always exists, but may not be efficient. The other always is O(1) and doesn't exist if that isn't possible. At least, that's how I remember it. – Jörg W Mittag Jan 11 '18 at 02:04
  • @JörgWMittag Depends on the object in question. On things like [`Array`](https://ruby-doc.org/core-2.5.0/Array.html#method-i-size) it's explicitly listed as "Alias for `length`". – tadman Jan 11 '18 at 06:13
  • Yeah, that always rubbed me the wrong way. IMO `size` should exist for objects that support it, and `length` should be an alias for `size` *if possible*, not the other way around. – Jörg W Mittag Jan 11 '18 at 09:55
  • However, I just noticed that `Enumerator` has `size` but not `length`. – Jörg W Mittag Jan 11 '18 at 09:57
  • Could be something the core team hasn't been paying attention to. Some elements of the core library are quite dated and need some review to see how consistent the naming conventions are. – tadman Jan 11 '18 at 17:36
2

exist? matches the convention used elsewhere throughout the stdlib, and goes back to the early days of ruby. For example array.include? (not includes?), string.match? (not matches?), object.respond_to? (not responds_to?). So in this light, File.exists? was always a blemish.

Some recommend that you read the dot as "does". So "if file does exist," "if array does include," "if string does match," etc.

George
  • 86
  • 2
0

I agree that if File.exists?('x.txt') reads much more natural than the plural form, which was probably Matz's intention for the alias. As far as I'm concerned, this particular deprecation was misguided.

However the general preference of a plural form may well be routed in handling enumarables/collections, where plural makes a sense when used with idioms like this:

pathnames.select(&:exist?)
zor-el
  • 320
  • 2
  • 11