Ruby重复时如何序列化字符串或递增字符串

发布于 2025-01-27 18:35:00 字数 993 浏览 1 评论 0原文

说明

我正在尝试采用对象的短语/名称,并在重复对象时序列化或递增名称。因此,例如,这里是模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两仪 2025-02-03 18:35:00

这是我提出的答案。

方法

def serialize_phrase(phrase)
  phrase.split("_").last.to_i == 0 ? phrase + '_1' : phrase.next
end

说明

该方法被称为so serialize_phrase(“ test”)

第一部分phrase.splate.split(“ _”)< /code>寻找下划线并创建所有部分的数组。在这种情况下,这是一个只有一个值[“ test”]的数组。 (它寻找下划线的原因是因为这是我通过添加下划线和数字来序列化短语的方式。)以下是一些示例:

phrase = "test"
phrase.split("_")    #  ["test"]
phrase = "test_1"
phrase.split("_")    #  ["test", "1"]
phrase = "test_1_3"
phrase.split("_")    #  ["test", "1", "3"]
phrase = "test1_14"
phrase.split("_")    #  ["test1", "14"]

第二部分.last返回了最后一个位置大批。使用上面的示例,您将获得此信息:

phrase = "test"
phrase.split("_").last    #  "test"
phrase = "test_1"
phrase.split("_").last    #  "1"
phrase = "test_1_3"
phrase.split("_").last    #  "3"
phrase = "test1_14"
phrase.split("_").last    #  "14"

第三部分.to_i将结果更改为整数。使用上面的示例,您会得到以下内容:

phrase = "test"
phrase.split("_").last.to_i    #  0
phrase = "test_1"
phrase.split("_").last.to_i    #  1
phrase = "test_1_3"
phrase.split("_").last.to_i    #  3
phrase = "test1_14"
phrase.split("_").last.to_i    #  14

注意数组中最后一部分的值如何无法转换为整数,则转换返回a 0值。这将帮助我们在下一部分中确定该短语是否需要通过1递增或附加“ _1”。通过检查值为0的值,我们从上面的示例中获得以下结果:

phrase = "test"
phrase.split("_").last.to_i == 0 ?   #  true
phrase = "test_1"
phrase.split("_").last.to_i == 0 ?   #  false
phrase = "test_1_3"
phrase.split("_").last.to_i == 0 ?   #  false
phrase = "test1_14"
phrase.split("_").last.to_i == 0 ?   #  false

下一部分短语 +'_1':phrase.next被设计为一行If语句。如果== 0?的结果是 true 然后短语 +'_1'被返回,else phrase.next退还。 短语 +'_1'只需将'_1'添加到评估的短语末尾即可。 .next是一个ruby类,可以增加字符串。

(旁注:) .next是一个方便的类,可以在字符串结束时确定完整整数,并增加1(so,'test_14'将为'test_15')。为了我的需求,此类的问题是,如果“ test”短语具有.next,则结果是“ tesu”。它可以做到这一点(获得下一个字母价值),但不是我想要的。

这是迄今为止使用的示例的结果。

phrase = "test"
phrase.split("_").last.to_i == 0 ? phrase + '_1' : phrase.next  #  "tesu"
phrase = "test_1"
phrase.split("_").last.to_i == 0 ? phrase + '_1' : phrase.next  #  "test_2"
phrase = "test_1_3"
phrase.split("_").last.to_i == 0 ? phrase + '_1' : phrase.next  #  "test_1_4"
phrase = "test1_14"
phrase.split("_").last.to_i == 0 ? phrase + '_1' : phrase.next  #  "test_15"

已知问题

如果短语为“ test_0”,则给出的结果将为“ test_0_1”,而不是“ test_1”。

Here is the answer that I have come up with.

METHOD

def serialize_phrase(phrase)
  phrase.split("_").last.to_i == 0 ? phrase + '_1' : phrase.next
end

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:

phrase = "test"
phrase.split("_")    #  ["test"]
phrase = "test_1"
phrase.split("_")    #  ["test", "1"]
phrase = "test_1_3"
phrase.split("_")    #  ["test", "1", "3"]
phrase = "test1_14"
phrase.split("_")    #  ["test1", "14"]

The second part .last returns the last location in the array. Using the examples above, you'd get this:

phrase = "test"
phrase.split("_").last    #  "test"
phrase = "test_1"
phrase.split("_").last    #  "1"
phrase = "test_1_3"
phrase.split("_").last    #  "3"
phrase = "test1_14"
phrase.split("_").last    #  "14"

The third part .to_i changes the result to an integer. Using the examples above, you'd get this:

phrase = "test"
phrase.split("_").last.to_i    #  0
phrase = "test_1"
phrase.split("_").last.to_i    #  1
phrase = "test_1_3"
phrase.split("_").last.to_i    #  3
phrase = "test1_14"
phrase.split("_").last.to_i    #  14

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:

phrase = "test"
phrase.split("_").last.to_i == 0 ?   #  true
phrase = "test_1"
phrase.split("_").last.to_i == 0 ?   #  false
phrase = "test_1_3"
phrase.split("_").last.to_i == 0 ?   #  false
phrase = "test1_14"
phrase.split("_").last.to_i == 0 ?   #  false

The next part phrase + '_1' : phrase.next is designed as a one line if statement. If the result of == 0 ? is true then phrase + '_1' is returned, else phrase.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.

phrase = "test"
phrase.split("_").last.to_i == 0 ? phrase + '_1' : phrase.next  #  "tesu"
phrase = "test_1"
phrase.split("_").last.to_i == 0 ? phrase + '_1' : phrase.next  #  "test_2"
phrase = "test_1_3"
phrase.split("_").last.to_i == 0 ? phrase + '_1' : phrase.next  #  "test_1_4"
phrase = "test1_14"
phrase.split("_").last.to_i == 0 ? phrase + '_1' : phrase.next  #  "test_15"

KNOWN PROBLEM

If the phrase is "test_0", then the result given will be "test_0_1", not "test_1".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文