When I print the hash, I see that it was populated correctly.
But when I run the #gsub on the message using that hash, it doesn’t
replace the matching keys in the string with their corresponding values.
What I don’t understand is why ciphers <= 13 are returning gibberish,
and why only ciphers > 13 actually do the cipher job accurately. I can
only assume I’m using the #gsub line inappropriately, but I don’t know
what the problem is.
But then again, if I run the same #gsub line with a different hash, it
works, so the problem is perhaps in how that line is dealing with the #times loop?
I thought somebody would notice that, but wasn’t sure how that fit into
my question. You see, when I cut moved_alphabet out of the code, the
cipher stopped working (the encoded message wouldn’t differ from the
input). So I left it in there. Don’t ask me why, that’s all I got.
It bothers me too, but
original_alphabet.each_with_index {|k,i|hash[k] = alphabet[i]}
should be putting out a=>, b=>b, c=>c etc. since moved_alphabet is what
is doing the rotating, and both original_alphabet and alphabet are
defined the same. (And this is exactly what it does when I cut
moved_alphabet out.)
When I print the hash, I get two alphabets, one altered, as keys and
values. when I run #gsub using that hash, it only uses that hash
accurately for the second half of the alphabet.
And thanks for the alternate methods (I think I understand the first
one; the second one less so), I’m sure there are better ways to do this
but what I really wanted to know was what was wrong with the code I did
write (other than being sub-optimal).
I’m using Geany to write and execute the code, and using the latest
version of Ruby, and I just don’t understand at all why this thing is
spitting out what it does.
when I cut moved_alphabet out of the code, the
cipher stopped working (the encoded message wouldn’t differ from the
input). So I left it in there. Don’t ask me why, that’s all I got.
your code contain:
moved_alphabet = cipher.times do
alphabet.rotate!
end