为什么选择这种方式而非其他方法来达成目标?
- 内容介绍
- 文章标签
- 相关推荐
本文共计299个文字,预计阅读时间需要2分钟。
最近我开始学习Ruby,并正在构建一个简单的加密方法。得到了想要的结果,但不确定原因。代码如下:
rubystring=This is a testoffset=5def encode(string, offset) coded= string.scan(/./) do |char| coded < 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”,但我不知道为什么.我期待一串数字,因为我从未指定将这些字母转回字母.有人可以解释一下发生了什么吗?String#<<
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.
原始字符串中的字符转换为序数整数,与偏移量一起添加,然后输入字符串#<<方法.
本文共计299个文字,预计阅读时间需要2分钟。
最近我开始学习Ruby,并正在构建一个简单的加密方法。得到了想要的结果,但不确定原因。代码如下:
rubystring=This is a testoffset=5def encode(string, offset) coded= string.scan(/./) do |char| coded < 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”,但我不知道为什么.我期待一串数字,因为我从未指定将这些字母转回字母.有人可以解释一下发生了什么吗?String#<<
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.
原始字符串中的字符转换为序数整数,与偏移量一起添加,然后输入字符串#<<方法.

