Reputation: 23
i am new to Ruby and working on a project to take in text from a file, convert the text to braille format, and then put the braille text into a new file.
Right now I am able to read the text, convert the entire string to an array. Then using a hash of English characters to braille, I am able to run map and replace the elements of the array with their braille equivalent.
I am having trouble interacting with the new array of Braille characters due to formatting issues.
starting_message = ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]
braille_converted_message = [
"0.\n00\n..",
"0.\n.0\n..",
"0.\n0.\n0.",
"0.\n0.\n0.",
"0.\n.0\n0.",
"..\n..\n..",
".0\n00\n.0",
"0.\n.0\n0.",
"0.\n00\n0.",
"0.\n0.\n0.",
"00\n.0\n.."]
I need to print the new array onto a file element by element including the correct formatting. i.e.
0.0.
00.0
....
The issue I am having is it either doesn't format correctly as in just appears as 0.\n00\n..
or puts in one vertical line.
Upvotes: 2
Views: 66
Reputation: 110725
One could do the following to minimize memory requirements (i.e., avoiding the construction of temporary arrays) without significantly compromising time-efficiency.
File.open('temp', 'w') do |f|
(braille_converted_message.first.count("\n") + 1).times do |i|
f.puts braille_converted_message.each_with_object('') do |row,str|
str << row[3*i,2]
end
end
end
puts File.read('temp')
0.0.0.0.0....00.0.0.00
00.00.0..0..00.0000..0
....0.0.0....00.0.0...
Upvotes: 0
Reputation: 114218
To write from left to right, you need to transpose your output. In order to do so, you have to split
each element, transpose
the result and join
them afterwards:
puts braille_converted_message.map(&:split)
.transpose
.map(&:join)
Output:
0.0.0.0.0....00.0.0.00
00.00.0..0..00.0000..0
....0.0.0....00.0.0...
Note that there's also a Unicode block for Braille Patterns:
⠓⠑⠇⠇⠕⠀⠺⠕⠗⠇⠙
Upvotes: 3