Returns the length of str
in bytes.
"\x80\u3042".bytesize #=> 4 "hello".bytesize #=> 5
Byte Reference—If passed a single Integer
, returns a substring of one byte at that position. If passed two Integer
objects, returns a substring starting at the offset given by the first, and a length given by the second. If given a Range
, a substring containing bytes at offsets given by the range is returned. In all three cases, if an offset is negative, it is counted from the end of str. Returns nil
if the initial offset falls outside the string, the length is negative, or the beginning of the range is greater than the end. The encoding of the resulted string keeps original encoding.
"hello".byteslice(1) #=> "e" "hello".byteslice(-1) #=> "o" "hello".byteslice(1, 2) #=> "el" "\x80\u3042".byteslice(1, 3) #=> "\u3042" "\x03\u3042\xff".byteslice(1..3) #=> "\u3042"
Returns an array of bytes in str. This is a shorthand for str.each_byte.to_a
.
If a block is given, which is a deprecated form, works the same as each_byte
.
Passes each byte in str to the given block, or returns an enumerator if no block is given.
"hello".each_byte {|c| print c, ' ' }
produces:
104 101 108 108 111
Calls the given block once for each byte (0..255) in ios, passing the byte as an argument. The stream must be opened for reading or an IOError
will be raised.
If no block is given, an enumerator is returned instead.
f = File.new("testfile") checksum = 0 f.each_byte {|x| checksum ^= x } #=> #<File:testfile> checksum #=> 12
This is a deprecated alias for each_byte
.
This is a deprecated alias for each_byte
.
Scans one byte and returns it. This method is not multibyte character sensitive. See also: getch
.
s = StringScanner.new('ab') s.get_byte # => "a" s.get_byte # => "b" s.get_byte # => nil $KCODE = 'EUC' s = StringScanner.new("\244\242") s.get_byte # => "\244" s.get_byte # => "\242" s.get_byte # => nil
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]
This is a deprecated alias for each_byte
.
Returns a random binary string containing size
bytes.
random_string = Random.new.bytes(10) # => "\xD7:R\xAB?\x83\xCE\xFAkO" random_string.size # => 10
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.
This is a deprecated alias for each_byte
.
Returns the total size in bytes of all the messages on the POP server.
This doesn’t yet handle encodings
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