ElektroStudios
ElektroStudios

Reputation: 20484

How to concatenate this in Ruby

I need help to concatenate 2 vars into 1 var, or any other method to use that vars together...

My intention is to make a art ascii generator where it needs to show a specific word to generate it... in this example i only will to show the word "a", but i can't, the print funciont prints the content of the var "([0,4])", i need to concatenate the vars and process it like a command, not like a string... :

# 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

Here is the difference:

   # This doesn't work:
   string = type + $long
   print string

Output:

Output

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

Output:

Output

Upvotes: 0

Views: 174

Answers (1)

Patrick Oscity
Patrick Oscity

Reputation: 54734

You can try to compose all the lines individually and then concatenate them with join("\n") like in the following example. I strongly recommend using a proper data structure for the char positions (or "longs" as you called them). In this example, i have used a hash.

# 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

the output of this program is:

         ##     
###  ##  #  ### 
#   # # ### ##  
### ###  #  ### 
        ##      

Upvotes: 6

Related Questions