Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

IO::Buffer#set_value

set_value(type, offset, value) → offset

Write to a buffer a value of type at offset. type should be one of symbols described in get_value.

Example
buffer = IO::Buffer.new(8)
#  =>
# #<IO::Buffer 0x0000555f5c9a2d50+8 INTERNAL>
# 0x00000000  00 00 00 00 00 00 00 00
buffer.set_value(:U8, 1, 111)
# => 1
buffer
#  =>
# #<IO::Buffer 0x0000555f5c9a2d50+8 INTERNAL>
# 0x00000000  00 6f 00 00 00 00 00 00                         .o......

Note that if the type is integer and value is Float, the implicit truncation is performed:

Example
buffer = IO::Buffer.new(8)
buffer.set_value(:U32, 0, 2.5)
buffer
#   =>
#  #<IO::Buffer 0x0000555f5c9a2d50+8 INTERNAL>
#  0x00000000  00 00 00 02 00 00 00 00
#                       ^^ the same as if we'd pass just integer 2
See also #set_string #size #resize #slice also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2