instance method
String#getbyte
(::int index) -> ::Integer?
getbyte(index) → integer or nil
string.c
View on GitHub ↗
VALUE
rb_str_getbyte(VALUE str, VALUE index)
{
long pos = NUM2LONG(index);
if (pos < 0)
pos += RSTRING_LEN(str);
if (pos < 0 || RSTRING_LEN(str) <= pos)
return Qnil;
return INT2FIX((unsigned char)RSTRING_PTR(str)[pos]);
}
Returns the byte at zero-based index as an integer:
Example
s = 'foo' s.getbyte(0) # => 102 s.getbyte(1) # => 111 s.getbyte(2) # => 111
Counts backward from the end if index is negative:
Example
s.getbyte(-3) # => 102
Returns nil if index is out of range:
Example
s.getbyte(3) # => nil s.getbyte(-4) # => nil
More examples:
Example
s = 'тест' s.bytes # => [209, 130, 208, 181, 209, 129, 209, 130] s.getbyte(2) # => 208 s = 'こんにちは' s.bytes # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175] s.getbyte(2) # => 147
Related: see Converting to Non-String.