The other converts work, what do I need to do to get D) working ok. Ultimately D) will be a cipher but I want to just get it working for each character for any function (downcase being an example) initially. The grouping into 5 character buckets is part of the cipher code I am developing.
def keystream_converter(message, conversion)
case conversion.downcase
when 'lower_case'
message.upcase
when 'upper_case'
message.downcase
when 'special'
message.each_char { |ltr| ltr.downcase }
else
'invalid_conversion'
end
end
initial_src = "I see Ruby going 100 years!!"
test_string = (initial_src.delete('^a-zA-Z') +"X"*(initial_src.length % 5)).scan(/.{5}/).to_s.upcase
lower = keystream_converter(test_string, 'lower_case')
upper = keystream_converter(test_string, 'upper_case')
special = keystream_converter(test_string, 'special')
#
puts "A) - " + initial_src
puts "B) - " + upper
puts "C) - " + lower
puts "D) - " + special
Output:
A) - I see Ruby going 100 years!!
B) - ["iseer", "ubygo", "ingye", "arsxx"]
C) - ["ISEER", "UBYGO", "INGYE", "ARSXX"]
D) - ["ISEER", "UBYGO", "INGYE", "ARSXX"]
String#each_char yields substrings of length 1 of the original string. If you change these substrings (even with
downcase!and not a non-mutatingdowncaselike in your case), original string will not be affected. If you want to useeach_charwith block, you can do something like this: