📜  红宝石 |字符串 each_str 方法

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

红宝石 |字符串 each_str 方法

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

示例 1:

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

输出:

R u b y Ruby
P r o g r a m m i n g Programming

示例 2:

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

输出:

S a m p l e Sample
#