What is the getbyte() method in Ruby?
Overview
In Ruby, the getbyte() method is used to return a byte or
Syntax
int getbyte( int index)
Parameters
This method takes the parameter index. It represents the index position of the character whose byte value we want to get.
Return value
It returns an integer value that is the byte representation of a character.
Code
# create a stringstr = "Edpresso";# get byte of some characters and printing themputs str.getbyte(0) # first characterputs str.getbyte(3) # fourth characterputs str.getbyte(7) # last character
Explanation
- Line 2: We create and initialize a string variable
str. - Lines 5–7: We call the
getbyte()method on the stringstrto get the bytes of several characters. We print these usingputs. Next, we pass the index position value of the characters togetbyte(). It returns the bytes of the characters.
When the code above is run, the byte value of the characters is printed to the console.