Skip to content

Commit 532cb80

Browse files
committedMar 30, 2016
minor refactoring of @mode handling
Doing this to ease the merging of the codedb-ffi-io branch later on. While fixing some code in rubysl-socket it was noted that it touched the @mode variable directly. To simplify compatibility (forward and backward) with that gem, it makes sense to move this @mode handling to its own methods.
1 parent 089e9eb commit 532cb80

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed
 

‎core/io.rb

+33-10
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ def initialize(fd, mode=undefined, options=undefined)
11691169
(@external || Encoding.default_external) == Encoding::ASCII_8BIT
11701170
@internal = nil
11711171
end
1172-
elsif @mode != RDONLY
1172+
elsif !mode_read_only?
11731173
if Encoding.default_external != Encoding.default_internal
11741174
@internal = Encoding.default_internal
11751175
end
@@ -1278,7 +1278,7 @@ def <<(obj)
12781278
def close_read
12791279
return if closed?
12801280

1281-
if @mode == WRONLY || @mode == RDWR
1281+
if mode_write_only? || mode_read_write?
12821282
raise IOError, 'closing non-duplex IO for reading'
12831283
end
12841284
close
@@ -1300,7 +1300,7 @@ def close_read
13001300
def close_write
13011301
return if closed?
13021302

1303-
if @mode == RDONLY || @mode == RDWR
1303+
if mode_read_only? || mode_read_write?
13041304
raise IOError, 'closing non-duplex IO for writing'
13051305
end
13061306
close
@@ -1636,19 +1636,17 @@ def eof?
16361636

16371637
def ensure_open_and_readable
16381638
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?
16411640
end
16421641

16431642
def ensure_open_and_writable
16441643
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?
16471645
end
16481646

16491647
def external_encoding
16501648
return @external if @external
1651-
return Encoding.default_external if @mode == RDONLY
1649+
return Encoding.default_external if mode_read_only?
16521650
end
16531651

16541652
##
@@ -1747,6 +1745,16 @@ def flush
17471745
self
17481746
end
17491747

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+
17501758
##
17511759
# Immediately writes all buffered data in ios to disk. Returns
17521760
# nil if the underlying operating system does not support fsync(2).
@@ -1831,6 +1839,21 @@ def lineno=(line_number)
18311839
@lineno = Integer(line_number)
18321840
end
18331841

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+
18341857
##
18351858
# FIXME
18361859
# Returns the process ID of a child process
@@ -2234,7 +2257,7 @@ def reopen(other, mode=undefined)
22342257
mode = @mode
22352258
# If this IO was already opened for writing, we should
22362259
# 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?
22382261
mode |= CREAT
22392262
end
22402263
else
@@ -2301,7 +2324,7 @@ def set_encoding(external, internal=nil, options=undefined)
23012324
when String
23022325
@external = nil
23032326
when nil
2304-
if @mode == RDONLY || @external
2327+
if mode_read_only? || @external
23052328
@external = nil
23062329
else
23072330
@external = Encoding.default_external

0 commit comments

Comments
 (0)
Please sign in to comment.