我最近开始学习 ruby并且正在构建一个简单的“加密”方法.我得到了理想的结果,但我不确定为什么. string = "This is a test"offset = 5def encode(string, offset) coded = "" string.scan(/./) do |char| numbers
string = "This is a test"
offset = 5
def encode(string, offset)
coded = ""
string.scan(/./) do |char|
numbers = char.ord
if numbers == 32
numbers = numbers
else
numbers = numbers + offset
end
coded << numbers
end
return coded
end
puts encode(string, offset)
我得到了所需的编码输出:“Ymnx nx f yjxy”,但我不知道为什么.我期待一串数字,因为我从未指定将这些字母转回字母.有人可以解释一下发生了什么吗?
Doc forString#<<
Append—Concatenates the given object to str. If the object is an Integer, it is considered as a codepoint, and is converted to a character before concatenation. Concat can take multiple arguments. All the arguments are concatenated in order.
原始字符串中的字符转换为序数整数,与偏移量一起添加,然后输入字符串#<<方法.
