@@ -1169,7 +1169,7 @@ def initialize(fd, mode=undefined, options=undefined)
1169
1169
( @external || Encoding . default_external ) == Encoding ::ASCII_8BIT
1170
1170
@internal = nil
1171
1171
end
1172
- elsif @mode != RDONLY
1172
+ elsif ! mode_read_only?
1173
1173
if Encoding . default_external != Encoding . default_internal
1174
1174
@internal = Encoding . default_internal
1175
1175
end
@@ -1278,7 +1278,7 @@ def <<(obj)
1278
1278
def close_read
1279
1279
return if closed?
1280
1280
1281
- if @mode == WRONLY || @mode == RDWR
1281
+ if mode_write_only? || mode_read_write?
1282
1282
raise IOError , 'closing non-duplex IO for reading'
1283
1283
end
1284
1284
close
@@ -1300,7 +1300,7 @@ def close_read
1300
1300
def close_write
1301
1301
return if closed?
1302
1302
1303
- if @mode == RDONLY || @mode == RDWR
1303
+ if mode_read_only? || mode_read_write?
1304
1304
raise IOError , 'closing non-duplex IO for writing'
1305
1305
end
1306
1306
close
@@ -1636,19 +1636,17 @@ def eof?
1636
1636
1637
1637
def ensure_open_and_readable
1638
1638
ensure_open
1639
- write_only = @mode & ACCMODE == WRONLY
1640
- raise IOError , "not opened for reading" if write_only
1639
+ raise IOError , "not opened for reading" if mode_write_only?
1641
1640
end
1642
1641
1643
1642
def ensure_open_and_writable
1644
1643
ensure_open
1645
- read_only = @mode & ACCMODE == RDONLY
1646
- raise IOError , "not opened for writing" if read_only
1644
+ raise IOError , "not opened for writing" if mode_read_only?
1647
1645
end
1648
1646
1649
1647
def external_encoding
1650
1648
return @external if @external
1651
- return Encoding . default_external if @mode == RDONLY
1649
+ return Encoding . default_external if mode_read_only?
1652
1650
end
1653
1651
1654
1652
##
@@ -1747,6 +1745,16 @@ def flush
1747
1745
self
1748
1746
end
1749
1747
1748
+ def force_read_only
1749
+ @mode = ( @mode & ~ACCMODE ) | RDONLY
1750
+ end
1751
+ private :force_read_only
1752
+
1753
+ def force_write_only
1754
+ @mode = ( @mode & ~ACCMODE ) | WRONLY
1755
+ end
1756
+ private :force_write_only
1757
+
1750
1758
##
1751
1759
# Immediately writes all buffered data in ios to disk. Returns
1752
1760
# nil if the underlying operating system does not support fsync(2).
@@ -1831,6 +1839,21 @@ def lineno=(line_number)
1831
1839
@lineno = Integer ( line_number )
1832
1840
end
1833
1841
1842
+ def mode_read_only?
1843
+ ( @mode & ACCMODE ) == RDONLY
1844
+ end
1845
+ private :mode_read_only?
1846
+
1847
+ def mode_read_write?
1848
+ ( @mode & ACCMODE ) == RDWR
1849
+ end
1850
+ private :mode_read_write?
1851
+
1852
+ def mode_write_only?
1853
+ ( @mode & ACCMODE ) == WRONLY
1854
+ end
1855
+ private :mode_write_only?
1856
+
1834
1857
##
1835
1858
# FIXME
1836
1859
# Returns the process ID of a child process
@@ -2234,7 +2257,7 @@ def reopen(other, mode=undefined)
2234
2257
mode = @mode
2235
2258
# If this IO was already opened for writing, we should
2236
2259
# create the target file if it doesn't already exist.
2237
- if ( mode & RDWR == RDWR ) || ( mode & WRONLY == WRONLY )
2260
+ if mode_read_write? || mode_write_only?
2238
2261
mode |= CREAT
2239
2262
end
2240
2263
else
@@ -2301,7 +2324,7 @@ def set_encoding(external, internal=nil, options=undefined)
2301
2324
when String
2302
2325
@external = nil
2303
2326
when nil
2304
- if @mode == RDONLY || @external
2327
+ if mode_read_only? || @external
2305
2328
@external = nil
2306
2329
else
2307
2330
@external = Encoding . default_external
0 commit comments