2012-03-25 22 views
0

我需要幫助來連接2個瓦爾成1個VAR或使用該瓦爾在一起的任何其他方法...如何連接這在Ruby中

我的目的是讓藝術ASCII碼發生器需要的地方展示一個特定的詞來產生它...在這個例子中,我只會顯示單詞「a」,但我不能,打印功能打印變量「([0,4])」的內容,我需要連接的VAR和處理它像一個命令,而不是像一個字符串...:

# 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 

輸出:

Output

# But this works :(
    print type[0,4], "\n" 

輸出:

Output

回答

6

你可以嘗試單獨組成的所有行,然後在下面與join("\n")將它們連接起來像例。我強烈建議爲字符位置使用適當的數據結構(或稱爲「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 

這個程序的輸出是:

  ##  
### ## # ### 
# # # ### ## 
### ### # ### 
     ##  
+0

三江源這麼多!現在我可以繼續與你的幫助 – ElektroStudios 2012-03-25 16:11:48

+0

@ user1248295然後你應該標記這個答案爲接受,投票箭頭下方的小勾號標記。 – 2012-03-25 17:18:41

+0

對不起,我不知道。 thx再次 – ElektroStudios 2012-03-25 21:51:22

相關問題