I'm new to Ruby and am trying to 'inject' key/value pairs to an existing hash in Ruby. I know you can do this with << for arrays, for e.g.
arr1 = []
a << "hello"
But could I do something similar for a hash? So something like
hash1 = {}
hash1 << {"a" => 1, "b" => 2}
Basically, I'm trying push key value pairs in a loop based on a condition.
# Encoder: This shifts each letter forward by 4 letters and stores it in a hash called cipher. On reaching the end, it loops back to the first letter
def encoder (shift_by)
alphabet = []
cipher = {}
alphabet = ("a".."z").to_a
alphabet.each_index do |x|
if (x+shift_by) <= 25
cipher = {alphabet[x] => alphabet[x+shift_by]}
else
cipher = {alphabet[x] => alphabet[x-(26-shift_by)]} #Need this piece to push additional key value pairs to the already existing cipher hash.
end
end
Sorry for pasting my whole method here. Can anyone please help me with this?