Project

General

Profile

Actions

Bug #10815

closed

Dir.foreach in block form does not allow encoding

Added by djberg96 (Daniel Berger) about 9 years ago. Updated about 9 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.0p0 (2014-12-25 revision 49005) [i386-mswin32_120]
[ruby-core:67938]

Description

It doesn't appear that Dir.foreach{} allows an encoding:

enc = Encoding::UTF_16LE

# Without a block, works ok
p Dir.foreach(Dir.pwd)      # => Enumerator
p Dir.foreach(Dir.pwd, enc) # => Enumerator

# With a block, fails if more than 1 argument is passed
p Dir.foreach(Dir.pwd){ }      # => nil
p Dir.foreach(Dir.pwd, enc){ } # => ArgumentError

Updated by djberg96 (Daniel Berger) about 9 years ago

I should also note that the method does not appear to do any sort of type or arity checking. I'm not sure if that's intentional.

Dir.foreach(Dir.pwd, true, false, nil) # => no error

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

  • Status changed from Open to Rejected

Encoding is a keyword argument.

Dir.foreach( dirname, encoding: enc ) {| filename | block }  -> nil
Dir.foreach(Dir.pwd, true, false, nil) # => no error

It returns an enumerator.

Dir.foreach( dirname )                                       -> an_enumerator

You'll see an ArgumentError when enumerating it.

Updated by djberg96 (Daniel Berger) about 9 years ago

Ok, but that seems a strange way to declare the function prototype. Compare that with Dir.entries, which does give you an error if you pass the wrong number of arguments.

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

Dir.entries doesn't return an enumerator.

Updated by djberg96 (Daniel Berger) about 9 years ago

I guess I'm not sure at what point or why the return type started governing function prototypes, but ok.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0