如何在Ruby中连接这个
我需要帮助将2个变量连接成1个变量,或者任何其他方法将这些变量一起使用 我的意图是制作一个art ascii生成器,它需要显示一个特定的单词来生成它。。。在本例中,我只想显示单词“a”,但我不能,print函数打印变量“([0,4])”的内容,我需要连接变量并像命令一样处理它,而不是像字符串一样…:如何在Ruby中连接这个,ruby,Ruby,我需要帮助将2个变量连接成1个变量,或者任何其他方法将这些变量一起使用 我的意图是制作一个art ascii生成器,它需要显示一个特定的单词来生成它。。。在本例中,我只想显示单词“a”,但我不能,print函数打印变量“([0,4])”的内容,我需要连接变量并像命令一样处理它,而不是像字符串一样…: # encoding:utf-8 threexfive = ' # # ## ## ### ### ### ### # ### # # # #
# encoding:utf-8
threexfive = '
# # ##
## ### ### ### ### # ###
# # # # # # # ## ### # #
### ### ### ### ### # ##
## ###
'
# The long of the letters a to f
threexfive_longs = '
a=[0,4]
b=[4,4]
c=[8,4]
d=[12,4]
e=[16,4]
f=[20,4]
'
string = ''
word = 'a'
word.each_char do |char|
$long = threexfive_longs.split(char).last.split(']').first.split('=').last + "]"
threexfive.each_line do |type|
# This don't works:
string = type + $long
print string
# But this works :(
# print type[20,4], type[0,4], type[8,4], type[16,4], "\n"
end
end
区别如下:
# This doesn't work:
string = type + $long
print string
输出:
# But this works :(
print type[0,4], "\n"
输出:
# But this works :(
print type[0,4], "\n"
您可以尝试单独组合所有行,然后使用
join(“\n”)
将它们连接起来,如以下示例所示。我强烈建议对字符位置(或您所称的“long”)使用适当的数据结构。在本例中,我使用了散列
# encoding:utf-8
threexfive = '
# # ##
## ### ### ### ### # ###
# # # # # # # ## ### # #
### ### ### ### ### # ##
## ###
'
char_pos = {
:a => 0..3,
:b => 4..7,
:c => 8..11,
:d => 12..15,
:e => 16..19,
:f => 20..23
}
word = 'cafe'
result = Array.new(threexfive.lines.count){''} # array with n empty lines
word.each_char do |char|
pos = char_pos[char.to_sym] # get position of char
threexfive.lines.each_with_index do |line,index|
next if index == 0 # first line of threexfive is empty -> skip
result[index] << line[pos] # compose individual lines
end
end
result = result.join("\n") # join the lines with \n (newline)
puts result # print result
您可以尝试单独组合所有行,然后使用
join(“\n”)
将它们连接起来,如以下示例所示。我强烈建议对字符位置(或您所称的“long”)使用适当的数据结构。在本例中,我使用了散列
# encoding:utf-8
threexfive = '
# # ##
## ### ### ### ### # ###
# # # # # # # ## ### # #
### ### ### ### ### # ##
## ###
'
char_pos = {
:a => 0..3,
:b => 4..7,
:c => 8..11,
:d => 12..15,
:e => 16..19,
:f => 20..23
}
word = 'cafe'
result = Array.new(threexfive.lines.count){''} # array with n empty lines
word.each_char do |char|
pos = char_pos[char.to_sym] # get position of char
threexfive.lines.each_with_index do |line,index|
next if index == 0 # first line of threexfive is empty -> skip
result[index] << line[pos] # compose individual lines
end
end
result = result.join("\n") # join the lines with \n (newline)
puts result # print result
非常感谢你!!!现在我可以继续项目了谢谢你的支持help@user1248295然后你应该把这个答案标记为接受,投票箭头下面的小记号。非常感谢!!!现在我可以继续项目了谢谢你的支持help@user1248295然后,您应该将此答案标记为已接受,即投票箭头下方的小勾号。