Ruby这样做的原因是什么?
- 内容介绍
- 文章标签
- 相关推荐
本文共计302个文字,预计阅读时间需要2分钟。
最近开始学习Ruby,并正在构建一个简单的加密方法。得到了想要的结果,但不确定原因。代码如下:
rubystring=This is a testoffset=5def encode(string, offset) coded= string.scan(/./) do |char| numbers=char.ord + offset coded +=numbers.chr end codedend
我最近开始学习 ruby并且正在构建一个简单的“加密”方法.我得到了理想的结果,但我不确定为什么.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.
原始字符串中的字符转换为序数整数,与偏移量一起添加,然后输入字符串#<<方法.
本文共计302个文字,预计阅读时间需要2分钟。
最近开始学习Ruby,并正在构建一个简单的加密方法。得到了想要的结果,但不确定原因。代码如下:
rubystring=This is a testoffset=5def encode(string, offset) coded= string.scan(/./) do |char| numbers=char.ord + offset coded +=numbers.chr end codedend
我最近开始学习 ruby并且正在构建一个简单的“加密”方法.我得到了理想的结果,但我不确定为什么.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.
原始字符串中的字符转换为序数整数,与偏移量一起添加,然后输入字符串#<<方法.

