Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns the byte at zero-based index as an integer:
s = 'abcde'  # => "abcde"
s.getbyte(0) # => 97
s.getbyte(1) # => 98
Related: String#setbyte.
 
               static 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]);
}
            

Was this page useful?