Ruby重复时如何序列化字符串或递增字符串
说明
我正在尝试采用对象的短语/名称,并在重复对象时序列化或递增名称。因此,例如,这里是模型:
create_table "forwarders", force: :cascade do |t|
t.bigint "site_id", null: false
t.string "phrase"
t.string "url"
t.integer "redirect_count", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["site_id"], name: "index_forwarders_on_site_id"
end
现在,这是我要复制的该模型的对象:
{
id: 19,
site_id: 1,
phrase: "test",
url: "https://google.com",
redirect_count: 9,
created_at: Tue, 10 May 2022 01:48:29.791141000 UTC +00:00,
updated_at: Tue, 10 May 2022 01:55:04.214172000 UTC +00:00
}
重复此内容时,我想增加pherse
与计算机复制时发生的情况相似并粘贴到同一文件夹。因此,新的短语
应为'test_1',或者如果已经'test_1',则应该成为'test_2'。
我的解决方案
我将在在线进行大量搜索后发布我想到的答案。这是我可以提出的最优雅的解决方案。但是,我的问题是...
问题
... Ruby已经有一个会这样做的字符串类?我找不到一个。
EXPLANATION
I am trying to take a phrase/name of an object and to serialize or to increment the name when the object is being duplicated. So, for instance here is the model:
create_table "forwarders", force: :cascade do |t|
t.bigint "site_id", null: false
t.string "phrase"
t.string "url"
t.integer "redirect_count", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["site_id"], name: "index_forwarders_on_site_id"
end
Now, here is the object of that model that I want to duplicate:
{
id: 19,
site_id: 1,
phrase: "test",
url: "https://google.com",
redirect_count: 9,
created_at: Tue, 10 May 2022 01:48:29.791141000 UTC +00:00,
updated_at: Tue, 10 May 2022 01:55:04.214172000 UTC +00:00
}
When duplicating this, I want to increment the phrase
similar to what happens on a computer when copying and pasting to the same folder. So, the new phrase
should be 'test_1', or if already 'test_1' it should become 'test_2'.
MY SOLUTION
I will post the answer I have come up with after doing much searching online. This is the most elegant solution that I could come up with; but, my question is...
QUESTION
...does Ruby already has a string Class that will do this? I have not been able to find one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我提出的答案。
方法
说明
该方法被称为so
serialize_phrase(“ test”)
第一部分
phrase.splate.split(“ _”)< /code>寻找下划线并创建所有部分的数组。在这种情况下,这是一个只有一个值
[“ test”]
的数组。 (它寻找下划线的原因是因为这是我通过添加下划线和数字来序列化短语的方式。)以下是一些示例:第二部分
.last
返回了最后一个位置大批。使用上面的示例,您将获得此信息:第三部分
.to_i
将结果更改为整数。使用上面的示例,您会得到以下内容:注意数组中最后一部分的值如何无法转换为整数,则转换返回a
0
值。这将帮助我们在下一部分中确定该短语是否需要通过1递增或附加“ _1”。通过检查值为0的值,我们从上面的示例中获得以下结果:下一部分
短语 +'_1':phrase.next
被设计为一行If语句。如果== 0?
的结果是 true 然后短语 +'_1'
被返回,elsephrase.next
退还。短语 +'_1'
只需将'_1'添加到评估的短语末尾即可。.next
是一个ruby类,可以增加字符串。(旁注:)
.next
是一个方便的类,可以在字符串结束时确定完整整数,并增加1(so,'test_14'将为'test_15')。为了我的需求,此类的问题是,如果“ test”短语具有.next
,则结果是“ tesu”。它可以做到这一点(获得下一个字母价值),但不是我想要的。这是迄今为止使用的示例的结果。
已知问题
如果短语为“ test_0”,则给出的结果将为“ test_0_1”,而不是“ test_1”。
Here is the answer that I have come up with.
METHOD
EXPLANATION
The method is called like so
serialize_phrase("test")
The first part
phrase.split("_")
looks for an underscore and creates an array of all parts. In this case it is an array with only one value["test"]
. (The reason it looks for an underscore is because that is how I am serializing the phrases, by adding an underscore and a number.) Here's some examples:The second part
.last
returns the last location in the array. Using the examples above, you'd get this:The third part
.to_i
changes the result to an integer. Using the examples above, you'd get this:Notice how if the value of the last part in the array is not able to be converted to an integer, then the conversion returns a
0
value. This will help us in the next part to determine if the phrase needs to be incremented by 1 or appended with '_1'. By checking for a value of 0, we get the following results from the examples above:The next part
phrase + '_1' : phrase.next
is designed as a one line if statement. If the result of== 0 ?
istrue
thenphrase + '_1'
is returned, elsephrase.next
is returned.phrase + '_1'
just adds '_1' to the end of the phrase that was evaluated..next
is a Ruby Class that increments a string.(Side Note:)
.next
is a handy Class that can determine the full integer at the end of a string and increase by 1 (So, 'test_14' will be 'test_15'). The problem with this Class for my needs is that if the phrase "test" has.next
called on it the result is "tesu". A nice thing for it to be able to do (get the next alphabetic value), but not what I am wanting.Here are the results of the examples that have been used so far.
KNOWN PROBLEM
If the phrase is "test_0", then the result given will be "test_0_1", not "test_1".