我如何将其转换为,然后从该数组中提取特定的内容?

发布于 2024-12-04 03:34:31 字数 956 浏览 1 评论 0原文

我将 Facebook 点赞保存在表中(它们位于字符串中),并在控制台中执行以下操作,以使用用户模型的 like 方法返回点赞:

<块引用>

用户.first.likes

=> "--- !seq:Koala::Facebook::GraphCollection \n- name: Rome Sweet Rome\n  category:
   Book\n  id: \"136333439795671\"\n  created_time: 2011-09-05T12:03:09+0000\n- name:
   Drawn Together\n category: Tv show\n  id: \"8694990902\"\n  created_time:
   2008-10-03T10:39:46+0000\n"

下面是 YAML 中的内容:

<块引用>

y 用户.first.likes

--- |
 --- !seq:Koala::Facebook::GraphCollection 
 - name: Rome Sweet Rome
 category: Book
 id: "136333439795671"
 created_time: 2011-09-05T12:03:09+0000
 - name: Drawn Together
 category: Tv show
 id: "8694990902"
 created_time: 2008-10-03T10:39:46+0000

 => nil

我希望最终结果给我类似的东西:

>> ["Rome sweet Rome", "Drawn Together"]

I have Facebook likes saved in a table (they're in a string) and do the following in the console to return the likes using the like method for the user model:

User.first.likes

=> "--- !seq:Koala::Facebook::GraphCollection \n- name: Rome Sweet Rome\n  category:
   Book\n  id: \"136333439795671\"\n  created_time: 2011-09-05T12:03:09+0000\n- name:
   Drawn Together\n category: Tv show\n  id: \"8694990902\"\n  created_time:
   2008-10-03T10:39:46+0000\n"

Below it is in YAML:

y User.first.likes

--- |
 --- !seq:Koala::Facebook::GraphCollection 
 - name: Rome Sweet Rome
 category: Book
 id: "136333439795671"
 created_time: 2011-09-05T12:03:09+0000
 - name: Drawn Together
 category: Tv show
 id: "8694990902"
 created_time: 2008-10-03T10:39:46+0000

 => nil

I want the end result to give me something like:

>> ["Rome sweet Rome", "Drawn Together"]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夜空下最亮的亮点 2024-12-11 03:34:31

将字符串拆分为单独的行,由 \n 字符分隔(或者如果它出现为字符串“\n”,则使用双引号对该字符串进行分隔)

like_elements = User.first.likes.split("\n") # <- String, not character, delimited version
=> ['id: "136333439795671"', 'created_time: 2011-09-05T12:03:09+0000", "- name: Drawn Together" ... etc.]

然后收集所有以将“- name:”放入自己的数组中:

name_elements = like_elements.select{|s| s.start_with?("- name: ")}
=> ["- name: Drawn Together", "- name: Rome sweet Rome"]

然后取出 name_elements 中的每个元素,并去掉前导的 "- name: " 文本,并删除前导和尾随的文本空白

names_of_likes_only = name_elements.collect{|n| n.gsub("- name: ", "").strip}
=> ["Drawn Together", "Rome sweet Rome"]

Split the string into separate lines, delimited by the \n character (or if it comes across as the string "\n", use double-quotes to delimit on that string)

like_elements = User.first.likes.split("\n") # <- String, not character, delimited version
=> ['id: "136333439795671"', 'created_time: 2011-09-05T12:03:09+0000", "- name: Drawn Together" ... etc.]

Then collect all elements that start with "- name: " into their own array:

name_elements = like_elements.select{|s| s.start_with?("- name: ")}
=> ["- name: Drawn Together", "- name: Rome sweet Rome"]

Then take each of the elements in name_elements and strip out the leading "- name: " text, and remove leading an trailing whitespace

names_of_likes_only = name_elements.collect{|n| n.gsub("- name: ", "").strip}
=> ["Drawn Together", "Rome sweet Rome"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文