ruby - How to join nested char array to string -
i have following code:
def caesar_cipher(text, move_by) move_by %= 26 chars = hash[('a'..'z').map.with_index.to_a] converted = text.split.map |word| word.chars.map |char| if (chars[char.downcase] + move_by) <= 26 chars.key(chars[char.downcase] + move_by) else chars.key(chars[char.downcase] + move_by - 26) end end end end print caesar_cipher("what string", 5)
it converts string variable text
integer. here output when run it: [["b", "m", "f", "y"], ["f"], ["x", "y", "w", "n", "s", "l"]]
, , i'd joined this"bmft f xywnsl"
. i've tried .join
method, gives me "bmftfxywnsl"
if:
arr = [["b", "m", "f", "y"], ["f"], ["x", "y", "w", "n", "s", "l"]]
then
arr.map(&:join).join(' ') #=> "bmfy f xywnsl"
you can think of map(&:join)
as:
arr.map { |a| a.join }.join(' ')
isn't ruby great?
Comments
Post a Comment