kwatch
9/17/2008 3:59:00 PM
Thank you, Matz.
2008/09/17 Yukihiro Matsumoto <m...@ruby-lang.org> wrote:
> |* Why is IO.read(filename) defined?
> | IMO, File.read() is more natural than IO.read() because
> | IO class is not related to filename, I think.
>
> No big reason. Historically IO works on files, i.e. IO.open(path),
> so that it was natural to provide these methods in IO too.
File.open() is different from IO.open().
IO.open(fd) takes file descriptor as argument,
while File.open(filename) takes filename.
irb(main):012:0> File.open('/tmp/hoge.txt') {|f| f.read }
=> "foo"
irb(main):013:0> IO.open('/tmp/hoge.txt') {|f| f.read }
TypeError: can't convert String into Integer
from (irb):13:in `initialize'
from (irb):13:in `open'
from (irb):13
IO.open() can't take filename as argument, so it is odd for me
that IO.read() is defined in IO class, not File class.
>
> |* Is there any reason that IO.write() (or File.write()) is not
> |provided?
> | I have to define File.write() for each project...
> | It is easy to define File.write() but I hope it is provided by Ruby.
>
> For File.read(), when you want to specify file mode, you can just say:
>
> File.read(path, "rb")
Ruby's manual says that 2nd argument of File.read() is length to read.
irb(main):016:0> File.read('/tmp/hoge.txt')
=> "foo"
irb(main):017:0> File.read('/tmp/hoge.txt', 2)
=> "fo"
irb(main):018:0> File.read('/tmp/hoge.txt', 'rb') # ERROR!
TypeError: can't convert String into Integer
from (irb):17:in `read'
from (irb):17
>
> For File.write(), it is more likely to be:
>
> File.write(path, str, "wb")
>
> which makes me feel weird. In 1.9, we can write
>
> File.write(path, str, mode: "wb")
>
> which I feel slightly better.
> matz.
You mean that there is no plan to add File.write() in Ruby 1.9?
--
regards,
makoto kuwata