Concatenates each object in objects
into self
; returns self
; performs no encoding validation or conversion:
s = 'foo' s.append_as_bytes(" \xE2\x82") # => "foo \xE2\x82" s.valid_encoding? # => false s.append_as_bytes("\xAC 12") s.valid_encoding? # => true
When a given object is an integer, the value is considered an 8-bit byte; if the integer occupies more than one byte (i.e,. is greater than 255), appends only the low-order byte (similar to String#setbyte
):
s = "" s.append_as_bytes(0, 257) # => "\u0000\u0001" s.bytesize # => 2
Related: see Modifying.
Calls the given block with each successive byte from self
; returns self
:
'hello'.each_byte {|byte| print byte, ' ' } print "\n" 'тест'.each_byte {|byte| print byte, ' ' } print "\n" 'こんにちは'.each_byte {|byte| print byte, ' ' } print "\n"
Output:
104 101 108 108 111 209 130 208 181 209 129 209 130 227 129 147 227 130 147 227 129 171 227 129 161 227 129 175
Returns an enumerator if no block is given.
Calls the given block with each byte (0..255) in the stream; returns self
. See Byte IO.
f = File.new('t.rus') a = [] f.each_byte {|b| a << b } a # => [209, 130, 208, 181, 209, 129, 209, 130] f.close
Returns an Enumerator
if no block is given.
Related: IO#each_char
, IO#each_codepoint
.
With a block given, calls the block with each remaining byte in the stream; see Byte IO.
With no block given, returns an enumerator.
Scans one byte and returns it as an integer. This method is not multibyte character sensitive. See also: getch
.
Peeks at the current byte and returns it as an integer.
s = StringScanner.new('ab') s.peek_byte # => 97
Iterates over each byte of each file in ARGV
. A byte is returned as an Integer
in the range 0..255.
This method allows you to treat the files supplied on the command line as a single file consisting of the concatenation of each named file. After the last byte of the first file has been returned, the first byte of the second file is returned. The ARGF.filename
method can be used to determine the filename of the current byte.
If no block is given, an enumerator is returned instead.
For example:
ARGF.bytes.to_a #=> [35, 32, ... 95, 10]
Returns a two-element array containing the beginning and ending byte-based offsets of the nth match. n can be a string or symbol to reference a named capture.
m = /(.)(.)(\d+)(\d)/.match("THX1138.") m.byteoffset(0) #=> [1, 7] m.byteoffset(4) #=> [6, 7] m = /(?<foo>.)(.)(?<bar>.)/.match("hoge") p m.byteoffset(:foo) #=> [0, 1] p m.byteoffset(:bar) #=> [2, 3]
Returns the offset (in bytes) of the beginning of the specified match.
When non-negative integer argument n
is given, returns the offset of the beginning of the n
th match:
m = /(.)(.)(\d+)(\d)/.match("THX1138.") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m[0] # => "HX1138" m.bytebegin(0) # => 1 m[3] # => "113" m.bytebegin(3) # => 3 m = /(т)(е)(с)/.match('тест') # => #<MatchData "тес" 1:"т" 2:"е" 3:"с"> m[0] # => "тес" m.bytebegin(0) # => 0 m[3] # => "с" m.bytebegin(3) # => 4
When string or symbol argument name
is given, returns the offset of the beginning for the named match:
m = /(?<foo>.)(.)(?<bar>.)/.match("hoge") # => #<MatchData "hog" foo:"h" bar:"g"> m[:foo] # => "h" m.bytebegin('foo') # => 0 m[:bar] # => "g" m.bytebegin(:bar) # => 2
Related: MatchData#byteend
, MatchData#byteoffset
.
Returns the offset (in bytes) of the end of the specified match.
When non-negative integer argument n
is given, returns the offset of the end of the n
th match:
m = /(.)(.)(\d+)(\d)/.match("THX1138.") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m[0] # => "HX1138" m.byteend(0) # => 7 m[3] # => "113" m.byteend(3) # => 6 m = /(т)(е)(с)/.match('тест') # => #<MatchData "тес" 1:"т" 2:"е" 3:"с"> m[0] # => "тес" m.byteend(0) # => 6 m[3] # => "с" m.byteend(3) # => 6
When string or symbol argument name
is given, returns the offset of the end for the named match:
m = /(?<foo>.)(.)(?<bar>.)/.match("hoge") # => #<MatchData "hog" foo:"h" bar:"g"> m[:foo] # => "h" m.byteend('foo') # => 1 m[:bar] # => "g" m.byteend(:bar) # => 3
Related: MatchData#bytebegin
, MatchData#byteoffset
.
Returns a random binary string containing size
bytes.
random_string = Random.new.bytes(10) # => "\xD7:R\xAB?\x83\xCE\xFAkO" random_string.size # => 10
Returns a random binary string. The argument size
specifies the length of the returned string.
if /foo #{bar}/ then end ^^^^^^^^^^^^
if /foo #{bar}/ then end ^^^^^^^^^^^^
Creates a new Socket::Option
object which contains a byte as data.
p Socket::Option.byte(:INET, :SOCKET, :KEEPALIVE, 1) #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
Returns the data in sockopt as an byte.
sockopt = Socket::Option.byte(:INET, :SOCKET, :KEEPALIVE, 1) p sockopt.byte => 1
See Zlib::GzipReader
documentation for a description.
Iterates over the buffer, yielding each byte starting from offset
.
If count
is given, only count
bytes will be yielded.
IO::Buffer.for("Hello World").each_byte(2, 2) do |offset, byte| puts "#{offset}: #{byte}" end # 2: 108 # 3: 108
True if version
satisfies this Requirement
.
Returns the discarded bytes when Encoding::InvalidByteSequenceError
occurs.
ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") begin ec.convert("abc\xA1\xFFdef") rescue Encoding::InvalidByteSequenceError p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "\xFF" on EUC-JP> puts $!.error_bytes.dump #=> "\xA1" puts $!.readagain_bytes.dump #=> "\xFF" end
Returns the bytes to be read again when Encoding::InvalidByteSequenceError
occurs.