是否可以将范围转换为字符串?我的想法是alpha
=("abcdefg..."
等)。我想这样做:
alpha.scan(/./) do |letters|
puts "I have five vowels", if letters.include?("a", "e", "i", "o, "u")
end
方法1:
alpha = ("a".."z").to_s # Still returns a range
方法2:
alpha = *("a".."z").to_s # Same; returns a range
答案 0 :(得分:5)
是的,试试:
("a".."z").to_a.join # => => "abcdefghijklmnopqrstuvwxyz"
to_a
将范围转换为数组。join
数组元素。希望有所帮助!