Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | String | bytes

概要

String#bytes -> [Integer]
String#bytes {|byte| ... } -> self

詳細

文字列の各バイトを数値の配列で返却。(self.each_byte.to_a と同じです)
ブロック版のインターフェースは obsolete (廃止) のため String#each_block を利用すること

サンプルコード
require 'tbpgr_utils'


bulk_puts_eval binding, <<-EOS
'hoge'.bytes
tmp = []
'hoge'.bytes { |e|tmp << (e + 1).chr }
tmp
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

'hoge'.bytes                           # => [104, 111, 103, 101]
tmp = []                               # => []
'hoge'.bytes { |e|tmp << (e + 1).chr } # => "hoge"
tmp                                    # => ["i", "p", "h", "f"]