Class: String
- Defined in:
- lib/zip/stdrubyext.rb,
lib/quiz1/t/solutions/Carlos/solitaire.rb
Overview
:nodoc:all
Instance Method Summary collapse
- #crypt(deck, decrypt = false) ⇒ Object
- #decrypt(deck) ⇒ Object
- #encrypt(deck) ⇒ Object
- #ends_with(aString) ⇒ Object
- #ensure_end(aString) ⇒ Object
- #lchop ⇒ Object
- #starts_with(aString) ⇒ Object
-
#to_numbers ⇒ Object
returns an array with the code of the letters, padded with the code of X.
Instance Method Details
#crypt(deck, decrypt = false) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/quiz1/t/solutions/Carlos/solitaire.rb', line 26 def crypt (deck, decrypt=false) numbers = to_numbers keystream = deck.generate_keystream numbers.length result = "" numbers.zip(keystream) do |n, k| k = -k if decrypt result << (n+k).to_letter end result end |
#decrypt(deck) ⇒ Object
41 42 43 |
# File 'lib/quiz1/t/solutions/Carlos/solitaire.rb', line 41 def decrypt (deck) crypt deck, true end |
#encrypt(deck) ⇒ Object
37 38 39 |
# File 'lib/quiz1/t/solutions/Carlos/solitaire.rb', line 37 def encrypt (deck) crypt deck, false end |
#ends_with(aString) ⇒ Object
40 41 42 |
# File 'lib/zip/stdrubyext.rb', line 40 def ends_with(aString) index(aString, -aString.size) end |
#ensure_end(aString) ⇒ Object
44 45 46 |
# File 'lib/zip/stdrubyext.rb', line 44 def ensure_end(aString) ends_with(aString) ? self : self + aString end |
#lchop ⇒ Object
48 49 50 |
# File 'lib/zip/stdrubyext.rb', line 48 def lchop slice(1, length) end |
#starts_with(aString) ⇒ Object
36 37 38 |
# File 'lib/zip/stdrubyext.rb', line 36 def starts_with(aString) rindex(aString, 0) == 0 end |
#to_numbers ⇒ Object
returns an array with the code of the letters, padded with the code of X
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/quiz1/t/solutions/Carlos/solitaire.rb', line 14 def to_numbers res=upcase.unpack("C*").collect { |b| if b.between? ?A, ?Z b - ?A + 1 else nil end }.compact # 24 == X res.fill 24, res.length, (5 - res.length % 5) % 5 end |