【问题标题】:Ruby create array form git log?Ruby 创建数组形式 git log?
【发布时间】:2014-08-19 00:25:45
【问题描述】:

我正在用 Ruby 编写代码以提取 git 作者列表,使用:

git log --since=#{from} --pretty="format:<author>%an</author>"

然而,这会返回一个像这样的字符串:

<author>first, last</author>
<author>first, last</author> 
...and so forth down the string...

如何在 Ruby 中将该字符串拆分为数组?

【问题讨论】:

  • 你想要一个数组中的名字和姓氏吗?
  • 我希望它拆分,以便每个元素包含 first last
  • 您可以尝试使用子字符串使用yourtext[minlength..maxlength] 划分列表,然后使用 for 循环将其添加到数组中。 !OBS! minlength 和 maxlength 是字母的个数,
  • 名字和姓氏发生变化。

标签: ruby arrays git split


【解决方案1】:

只需使用 split 来分割换行符:

authors_string = `git log --since=#{from} --pretty="format:<author>%an</author>"`
authors_array  = authors_string.split($/)

我在这里使用$/,它包含您操作系统的行尾。

【讨论】:

    【解决方案2】:
    # 's' contains authors, the string returned by 'git log'
    s.split "\n"
    => ["<author>first last</author>", "<author>first last</author>"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多