📜  红宝石 | String each_byte 方法

📅  最后修改于: 2022-05-13 01:54:30.422000             🧑  作者: Mango

红宝石 | String each_byte 方法

each_byte是 Ruby 中的 String 类方法,用于将给定字符串中的每个字节传递给给定块,如果没有给出块,则返回枚举器。

示例 1:

# Ruby program to demonstrate 
# the each_byte method 
       
# Taking a string and 
# using the method
puts "Sample".each_byte{|b| print b, ' ' }
puts "Input".each_byte{|b| print b, ' ' }

输出:

83 97 109 112 108 101 Sample
73 110 112 117 116 Input

示例 2:

# Ruby program to demonstrate 
# the each_byte method 
       
# Taking a string and 
# using the method
puts "Ruby".each_byte{|b| print b, ' ' }
puts "Programming".each_byte{|b| print b, ' ' }

输出:

82 117 98 121 Ruby
80 114 111 103 114 97 109 109 105 110 103 Programming