Passing Blocks as Arguments

#In the Encrypt class def rotate_message(message) 
indices_and_rotators = which_rotator(message)
new_indices = []
indices_and_rotators.length.times do
new_indices << (indices_and_rotators[i][0] + (indices_and_rotators[i][1] % 85))
end
end
def rotate_encrypted_message(message)
indices_and_rotators = which_rotator(message)
new_indices = []
indices_and_rotators.length.times do
new_indices << (indices_and_rotators[i][0] - (indices_and_rotators[i][1] % 85))
end
end
#this is in the class from which both Encrypt and Decrypt inheritdef rotate_indices(message, &block) 
indices_and_rotators = which_rotator(message)
@new_indices = []
indices_and_rotators.length.times do |i|
@new_indices << block.call(indicies_and_rotators[i][0], indices_and_rotators[i][1] % 85)
end
ensure_valid_rotator
end
#in Encrypt def rotate_message(message)
rotate_indices(message) do |initial, rotation|
initial + rotation
end
end
#in Decrypt def rotate_message(message)
rotate_indices(message) do |initial, rotation|
initial - rotation
end
end
class Array 
def accumulate(&block)
result = []
self.each do |item|
result << block.call(item)
end
result
end
end
#In use: [1, 2, 3].accumulate do |number|
number * number
end
# => [1, 4, 9]

Senior Software Engineer | www.adriennedomingus.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adrienne Domingus

Adrienne Domingus

Senior Software Engineer | www.adriennedomingus.com

More from Medium

How to install Ruby with rbenv in Windows 11 WSL-Ubuntu 20.04 distro

Using “Belongs_to” to Write Better Rails Tests With (or Without) Associations

Concurrent Programming with Go & Ruby

Deploy Rails App on DigitalOcean Droplet