abcreddy
abcreddy

Reputation: 102

Method to Encrypt the given string with the given number in Ruby

Can anyone help me to write a method which takes 2 parameters (one string and one number). the method should return the string encrypted with the given number.

For Example:

My String: ABCDE My Number: 3 Output Should Be: DEFGH

Note: String and Number are not constant.

Upvotes: 0

Views: 73

Answers (1)

DNNX
DNNX

Reputation: 6255

s = "ABCDE"
n = 3
s.chars.map{|ch| (ch.ord + n).chr}.join
# => "DEFGH"

Upvotes: 4

Related Questions