我需要帮助将2个变量连接成1个var,或者任何其他方法将这些变量连接在一起...
我的目的是制作一个艺术ascii发生器,它需要显示一个特定的单词来生成它...在这个例子中我只会显示单词“a”,但我不能,打印功能打印var“([0,4])”的内容,我需要连接变量并像处理命令一样处理它,而不是像字符串......:
# 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"
输出:
答案 0 :(得分:6)
您可以尝试单独编写所有行,然后将其与join("\n")
连接,如下例所示。我强烈建议为char位置使用适当的数据结构(或称为“longs”)。在这个例子中,我使用了哈希。
# 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
该程序的输出是:
##
### ## # ###
# # # ### ##
### ### # ###
##