tailieunhanh - Best of Ruby Quiz Pragmatic programmers phần 7

Nếu bạn so sánh với những người có các bài kiểm tra riêng của mình, bạn sẽ thấy rằng tôi đã không thực sự phải làm bất kỳ suy nghĩ. Những trường hợp thử nghiệm đã được trao cho tôi miễn phí. Làm thế nào tôi biết được câu trả lời cho các trường hợp thử nghiệm được mã hóa trước khi tôi đã có một chương trình làm việc? | Answer 15. Solitaire Cipher 168 def te st_encrypt assert_equal GLNCQ MJAFF FVOMB JIYCB @ Code in Ruby live longer end def te st_decrypt assert_equal CODEI NRUBY LIVEL ONGER @ GLNCQ MJAFF FVOMB JIYCB @ assert_equal YOURC IPHER ISWOR KINGX @ CLEPK HHNIY CFPWH FDFEH @ assert_equal WELCO METOR UBYQU IZXXX @ ABVAW LWZSY OORYK DUPVH end end If you compare those with the quiz itself you will see that I haven t really had to do any thinking yet. Those test cases were given to me for free. How did i know the answers to the encrypted test cases before i had a working program it s not just that i m in close with the quiz creator i assure you. i validated them with a deck of cards. There s no shame in a low-tech by-hand dry run to make sure you understand the process you are about to teach to a computer. The only decisions i have made so far are interface decisions. Running the cipher seems logically separate from keystream generation so i decided that each would receive its own class and the latter could be passed to the constructor of the former. This makes it possible to build ciphers using a completely different method of keystream generation. You can see that i mostly skip resolving what a keystream object will be at this point. i haven t come to that part yet after all. instead i just build a generic object and use Ruby s singleton class syntax to add a couple of methods to it. Don t panic if you ve never seen that syntax before it s just a means to add a couple of methods to a single The next_letter method will be the only interface method Cipher cares about and reset is just a tool for testing. Now we need to go from tests to implementation 40For a more detailed explanation see http ruby7SingletonTutorial. Report erratum Answer 15. Solitaire Cipher 169 solitaire_cipher class Cipher def chars char char A - 1 .chr .

TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG