为什么选择这种方式而非其他方法来达成目标?

2026-04-10 07:321阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计299个文字,预计阅读时间需要2分钟。

为什么选择这种方式而非其他方法来达成目标?

最近我开始学习Ruby,并正在构建一个简单的加密方法。得到了想要的结果,但不确定原因。代码如下:

rubystring=This is a testoffset=5def encode(string, offset) coded= string.scan(/./) do |char| coded < 我最近开始学习 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 for 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 < 我最近开始学习 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 for 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.

原始字符串中的字符转换为序数整数,与偏移量一起添加,然后输入字符串#<<方法.