🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 ## to Diamond in the Ruby Track

Published at Jul 17 2020 · 0 comments
Instructions
Test suite
Solution

The diamond kata takes as its input a letter, and outputs it in a diamond shape. Given a letter, it prints a diamond starting with 'A', with the supplied letter at the widest point.

## Requirements

• The first row contains one 'A'.
• The last row contains one 'A'.
• All rows, except the first and last, have exactly two identical letters.
• All rows have as many trailing spaces as leading spaces. (This might be 0).
• The diamond is horizontally symmetric.
• The diamond is vertically symmetric.
• The diamond has a square shape (width equals height).
• The letters form a diamond shape.
• The top half has the letters in ascending order.
• The bottom half has the letters in descending order.
• The four corners (containing the spaces) are triangles.

## Examples

In the following examples, spaces are indicated by `·` characters.

Diamond for letter 'A':

``````A
``````

Diamond for letter 'C':

``````··A··
·B·B·
C···C
·B·B·
··A··
``````

Diamond for letter 'E':

``````····A····
···B·B···
··C···C··
·D·····D·
E·······E
·D·····D·
··C···C··
···B·B···
····A····
``````

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby diamond_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride diamond_test.rb
``````

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### diamond_test.rb

``````require 'minitest/autorun'
require_relative 'diamond'

class DiamondTest < Minitest::Test
def test_letter_a
end

def test_letter_c
skip
string = "  A  \n"\
" B B \n"\
"C   C\n"\
" B B \n"\
"  A  \n"
end

def test_letter_e
skip
string = "    A    \n"\
"   B B   \n"\
"  C   C  \n"\
" D     D \n"\
"E       E\n"\
" D     D \n"\
"  C   C  \n"\
"   B B   \n"\
"    A    \n"
end
end``````
``````class Diamond
def self.make_diamond(arg)
throw :ArgumentError if arg.length > 1
max_dis = arg.bytes - 'A'.bytes
cur_dis = 0

str = ""
max_dis.times {str << " "}
str << "A"
max_dis.times {str << " "}
str << "\n"

if max_dis == 0
return str
end

for cur_dis in 1..max_dis
(max_dis - cur_dis).times {str << " "}
str << ("A".bytes + cur_dis).chr.to_s
(cur_dis*2 - 1).times {str << " "}
str << ("A".bytes + cur_dis).chr.to_s
(max_dis - cur_dis).times {str << " "}
str << "\n"
end

for cur_dis in (max_dis - 1)..1
(max_dis - cur_dis).times {str << " "}
str << ("A".bytes + cur_dis).chr.to_s
(cur_dis*2 - 1).times {str << " "}
str << ("A".bytes + cur_dis).chr.to_s
(max_dis - cur_dis).times {str << " "}
str << "\n"
end

max_dis.times {str << " "}
str << "A"
max_dis.times {str << " "}
str << "\n"

return str
end
end``````