latest_also_accomodate_lack_of_mode_param.diff

Roger Pack, 05/18/2011 03:37 AM

Download (5.59 KB)

View differences:

prelude.rb (working copy)
29 29
    }
30 30
  end
31 31
end
32

  
33

  
34
class IO
35
#
36
#  call-seq:
37
#     IO.write(name, string, [offset] )   => fixnum
38
#     IO.write(name, string, [offset], open_args )   => fixnum
39
#
40
#  Opens the file, optionally seeks to the given <i>offset</i>, writes
41
#  <i>string</i>, then returns the length written.
42
#  <code>write</code> ensures the file is closed before returning.
43
#  If <i>offset</i> is not given, the file is truncated.  Otherwise,
44
#  it is not truncated.
45
#
46
#  If the last argument (open_args) is a hash, it specifies options for the internal
47
#  call to open().  The keys would be the following.  open_args: is exclusive
48
#  to others, meaning that if you specify an element, it will use that instead
49
#  of the default.
50
#
51
#   encoding: string or encoding
52
#
53
#    specifies encoding of the read string.  encoding will be ignored
54
#    if length is specified.
55
#
56
#   mode: string
57
#
58
#    specifies mode argument for open().  it should start with "w" or "a" or "r+"
59
#    otherwise it would cause error.
60
#
61
#   perm: fixnum
62
#
63
#    specifies perm argument for open().
64
#
65
#   open_args: array of strings
66
#
67
#    specifies arguments for open() as an array.
68
#
69
#     IO.write("testfile", "0123456789")      #=> "0123456789"
70
#     IO.write("testfile", "0123456789", 20)  #=> "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
71
#
72
#   More examples:
73
#     File.write(path, "foo", encoding: "UTF-8")
74
#     File.write(path, "foo", 10, encoding: "UTF-8") # insert "foo" at offset 10
75
#
76
  def self.write name, string, offset = nil, open_args_hash = nil
77
    # 3rd param is either offset or open_args
78
    if !open_args_hash && offset.is_a?(Hash)
79
      open_args_hash = offset
80
      offset = nil
81
    end
82
    open_args_hash ||= {}
83
    if offset && File.exist?(name)
84
      open_args_hash[:mode] ||= "r+" # File::RDWR|File::CREAT
85
    else
86
      open_args_hash[:mode] ||= "w"  # File::WRONLY|File::TRUNC|File::CREAT
87
    end
88
    open(name, open_args_hash) do |f|
89
      if offset
90
        f.seek offset
91
      end
92
      f.write string
93
    end
94
  end
95
  
96
#  call-seq:
97
#     IO.binwrite(name, string, [offset] )   => fixnum
98
#
99
#  Opens the file, optionally seeks to the given <i>offset</i>, write
100
#  <i>string</i> then returns the length written.
101
#  <code>binwrite</code> ensures the file is closed before returning.
102
#  The open mode will be "wb:ASCII-8BIT".
103
#  If <i>offset</i> is not given, the file is truncated.  Otherwise,
104
#  it is not truncated.
105
#
106
#     IO.binwrite("testfile", "0123456789")      #=> "0123456789"
107
#     IO.binwrite("testfile", "0123456789", 20)  #=> "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
108
#
109
  def self.binwrite name, string, offset = nil
110
    open_args = offset ? (File::RDWR|File::CREAT|File::BINARY) : 'wb'
111
    open(name, open_args) do |f|
112
      if offset
113
        f.seek offset
114
      end
115
      f.write string
116
    end
117
  end
118
end
test/ruby/test_io.rb (working copy)
1900 1900
    end
1901 1901
    rescue NotImplementedError
1902 1902
  end
1903
  
1904
  
1905
  def test_s_write
1906
    t = Tempfile.new("foo")
1907
    path = t.path
1908
    t.close(false)
1909
    File.write(path, "foo\nbar\nbaz")
1910
    assert_equal("foo\nbar\nbaz", File.read(path))
1911
    File.write(path, "FOO", 0)
1912
    assert_equal("FOO\nbar\nbaz", File.read(path))
1913
    File.write(path, "BAR")
1914
    assert_equal("BAR", File.read(path))
1915
    File.write(path, "\u{3042}", mode: "w", encoding: "EUC-JP")
1916
    assert_equal("\u{3042}".encode("EUC-JP"), File.read(path, encoding: "EUC-JP"))
1917
    File.delete t
1918
    assert_equal(6, File.write(path,'string',2))
1919
    File.delete t
1920
    assert_raise(Errno::EINVAL) { File.write('/tmp/nonexisting','string',-2) }
1921
    assert_equal(6, File.write(path, 'string'))
1922
    assert_equal(3, File.write(path, 'sub', 1))
1923
    assert_equal("ssubng", File.read(path))
1924
    File.delete t
1925
    assert_equal(3, File.write(path, "foo", encoding: "UTF-8"))
1926
    File.delete t
1927
    assert_equal(3, File.write(path, "foo", 0, encoding: "UTF-8"))
1928
    assert_equal("foo", File.read(path))
1929
    assert_equal(1, File.write(path, "f", 1, encoding: "UTF-8"))
1930
    assert_equal("ffo", File.read(path))
1931
    File.delete t
1932
    assert_equal(1, File.write(path, "f", 1, encoding: "UTF-8"))
1933
    assert_equal("\00f", File.read(path))
1934
    assert_equal(1, File.write(path, "f", 0, encoding: "UTF-8"))
1935
    assert_equal("ff", File.read(path))
1936
    t.unlink
1937
  end
1938

  
1939
  def test_s_binwrite
1940
    t = Tempfile.new("foo")
1941
    path = t.path
1942
    t.close(false)
1943
    File.binwrite(path, "foo\nbar\nbaz")
1944
    assert_equal("foo\nbar\nbaz", File.read(path))
1945
    File.binwrite(path, "FOO", 0)
1946
    assert_equal("FOO\nbar\nbaz", File.read(path))
1947
    File.binwrite(path, "BAR")
1948
    assert_equal("BAR", File.read(path))
1949
    File.binwrite(path, "\u{3042}")
1950
    assert_equal("\u{3042}".force_encoding("ASCII-8BIT"), File.binread(path))
1951
    File.delete t
1952
    assert_equal(6, File.binwrite(path,'string',2))
1953
    File.delete t
1954
    assert_equal(6, File.binwrite(path, 'string'))
1955
    assert_equal(3, File.binwrite(path, 'sub', 1))
1956
    assert_equal("ssubng", File.binread(path))
1957
    assert_equal(6, File.size(path))
1958
    assert_raise(Errno::EINVAL) { File.binwrite('/tmp/nonexisting','string',-2) }
1959
    assert_raise(TypeError) { File.binwrite(path, "string", mode: "w", encoding: "EUC-JP") }
1960
    t.unlink
1961
  end
1962

  
1903 1963
end