Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to bytes in Ruby?

Tags:

ruby

How do I extend the String class, and attach a method named to_bytes?

like image 972
why Avatar asked Nov 05 '10 06:11

why


1 Answers

String#bytes returns enumerator through string bytes.

"asd".bytes => [97, 115, 100] 

In Ruby 1.9.3 the #bytes was returning an Enumerator so you had to add .to_a to convert it to an Array. Since 2.3 or maybe even earlier you don't have to add it anymore.

like image 81
Nakilon Avatar answered Sep 22 '22 03:09

Nakilon