diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..98dcead --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source 'https://rubygems.org' + +gem 'asciidoctor' +gem 'asciidoctor-pdf' +gem 'pygments.rb' diff --git a/crypto-square/README.adoc b/crypto-square/README.adoc new file mode 100644 index 0000000..3d7b565 --- /dev/null +++ b/crypto-square/README.adoc @@ -0,0 +1,67 @@ += Notes on Crypto Square as inspired by Exercism +:author: Gemp +:description: A discussion of Crypto Square as offered by Exercism. +:sectanchors: <.> +:safe: SERVER +:date: 26 October 2021 +:copyright: 2021 +:source-highlighter: pygments +:source-language: ruby +:pygments-style: manni +:pygments-linenums-mode: inline +:toc: right +:icons: font + +This document is focused on Ruby, using the exercise called Crypto Square on Exercism. +The current version of the exercise description may have changed.footnote:cryptosquare[https://github.com/exercism/ruby/blob/main/exercises/practice/crypto-square/.docs/instructions.md] + +.Description of Crypto Square +**** +As it was when solved, the documentation was written as: + +include::https://raw.githubusercontent.com/exercism/ruby/main/exercises/practice/crypto-square/.docs/instructions.md[leveloffset=+1] +**** + +<<< +== Crypto Square Iteration 1 + +=== Version 1 + + +[TIP] +Indentation in Ruby is two spaces, not tab, not four spaces. +We can use a variance from consistent style, though, to bring attention to an area when needed. +A very nice side effect from consistent style adherence! + +.crypto_square_1.rb +[source, ruby, linenums] +---- +include::crypto_square_1.rb[] +---- +<1> Indentation in Ruby is two spaces, so this is contrary to convention +<2> Explanation of why we would want to freeze this, and if it is immune to garbage collection + +<<< +== Crypto Square iteration 2 + +=== Version 2 + +This is the second iteration as submitted to Exercism. + +.crypto_square_2.rb +[source, ruby, linenums] +---- +include::crypto_square_2.rb[] +---- + +<<< +== Crypto Square Version 3 + +=== Version 3 + +.cryto_square_2_bis.rb +[source, ruby, linenums] +---- +include::crypto_square_2_bis.rb[] +---- + diff --git a/crypto-square/README.html b/crypto-square/README.html new file mode 100644 index 0000000..e0ceb9b --- /dev/null +++ b/crypto-square/README.html @@ -0,0 +1,718 @@ + + + + + + + + + + +Notes on Crypto Square as inspired by Exercism + + + + + + + +
+
+
+
+

This document is focused on Ruby, using the exercise called Crypto Square on Exercism. +The current version of the exercise description may have changed.[1]

+
+
+
+
Description of Crypto Square
+
+

As it was when solved, the documentation was written as:

+
+ +
+
+
+
+
+
+

Crypto Square Iteration 1

+
+
+

Version 1

+
+ + + + + +
+ + +Indentation in Ruby is two spaces, not tab, not four spaces. +We can use a variance from consistent style, though, to bring attention to an area when needed. +A very nice side effect from consistent style adherence! +
+
+
+
crypto_square_1.rb
+
+
 1class Crypto
+ 2
+ 3private (1)
+ 4
+ 5  attr_reader :text
+ 6
+ 7  def initialize(text)
+ 8    @text = text.downcase.scan(/\w/).freeze (2)
+ 9  end
+10
+11  def cipher
+12    return text if text.empty?
+13    chars = Math.sqrt(text.size).ceil
+14    length = chars * (text.size / chars.to_f).ceil
+15    (0...chars).map do |start|
+16      (start...length).step(chars).map { |i| text.fetch(i, ' ') }.join
+17    end
+18  end
+19
+20public
+21
+22  def ciphertext
+23    cipher.join(' ')
+24  end
+25
+26end
+
+
+
+ + + + + + + + + +
1Indentation in Ruby is two spaces, so this is contrary to convention
2Explanation of why we would want to freeze this, and if it is immune to garbage collection
+
+
+
+
+
+
+

Crypto Square iteration 2

+
+
+

Version 2

+
+

This is the second iteration as submitted to Exercism.

+
+
+
crypto_square_2.rb
+
+
 1class Crypto
+ 2
+ 3private
+ 4
+ 5  attr_reader :text
+ 6
+ 7  def initialize(text)
+ 8    @text = text.downcase.gsub(/\W/, '').freeze
+ 9  end
+10
+11  def cipher
+12    cols = Math.sqrt(text.size).ceil
+13    text.scan(/.{1,#{cols}}/).map { |s| s.ljust(cols).chars }.transpose
+14  end
+15
+16public
+17
+18  def ciphertext
+19    return text if text.empty?
+20    @cipher ||= cipher.map(&:join).join(' ')
+21  end
+22
+23end
+
+
+
+
+
+
+
+

Crypto Square Version 3

+
+
+

Version 3

+
+
cryto_square_2_bis.rb
+
+
 1class Crypto
+ 2
+ 3private
+ 4
+ 5  attr_reader :text
+ 6
+ 7  def initialize(text)
+ 8    @text = text.downcase.gsub(/\W/, '').freeze
+ 9  end
+10
+11  def cipher
+12    cols = Math.sqrt(text.size).ceil
+13    miss = text.size.modulo(cols) #avoid ljust on every string (comment)
+14    adjusted = miss.zero? ? text : text + ' ' * (cols - miss)
+15    adjusted.scan(/.{1,#{cols}}/).map(&:chars).transpose
+16  end
+17
+18public
+19
+20  def ciphertext
+21    return text if text.empty?
+22    @cipher ||= cipher.map(&:join).join(' ')
+23  end
+24
+25end
+
+
+
+
+
+
+
+
+
+1. https://github.com/exercism/ruby/blob/main/exercises/practice/crypto-square/.docs/instructions.md +
+
+ + + \ No newline at end of file diff --git a/crypto-square/crypto_square_1.rb b/crypto-square/crypto_square_1.rb index 0abcd37..e393f1d 100644 --- a/crypto-square/crypto_square_1.rb +++ b/crypto-square/crypto_square_1.rb @@ -1,11 +1,11 @@ class Crypto -private +private # <1> attr_reader :text def initialize(text) - @text = text.downcase.scan(/\w/).freeze + @text = text.downcase.scan(/\w/).freeze # <2> end def cipher @@ -23,4 +23,4 @@ def ciphertext cipher.join(' ') end -end \ No newline at end of file +end