defgrille(message,code)output=""# convert code to binarybinary_code=code.to_s(2)# prepend the appropriate number of zeros to match length of messagewhilemessage.length>binary_code.lengthbinary_code.prepend('0')end# combine messages and binary_code into a hasharrays_hash=Hash[message.split("").zip(binary_code.split(""))]# if the value is 1, add the key to the output stringarrays_hash.eachdo|k,v|ifv=="1"output<<kendendreturnoutputend
ruby
Ruby allows string interpolation in format strings, which makes generating
binary_code
easier:Ruby 2.7 also added a handy
filter_map
method, which can simplify the second half of your method: