创建一个人类可读的列表,在ruby列表的最后一个元素之前插入“and”

时间:2022-11-24 21:12:31

How would you take a list and make it into a comma-separated string, with "and" before the last element in the array?

你如何获取一个列表并将其变成逗号分隔的字符串,在数组的最后一个元素之前加上“和”?

Take something like:

采取类似的方式:

list1 = ['a','b','c']

And turn it into this:

把它变成这个:

=> "a, b, and c"

I remember ruby had a method for this. I've searched however, and couldn't find it. Thanks for the help.

我记得ruby有一个方法。然而,我搜索过,找不到它。谢谢您的帮助。

3 个解决方案

#1


15  

Try: [list[0...-1].join(", "), list.last].join(", and ").

尝试:[list [0 ...- 1] .join(“,”),list.last] .join(“,and”)。

Edit: Rails has the method you were probably looking for, called to_sentence.

编辑:Rails有你可能正在寻找的方法,称为to_sentence。

In case you do not have Rails or do not wish to depend on Rails, open Array class and include the above method, like:

如果您没有Rails或者不希望依赖Rails,请打开Array类并包含上述方法,如:

class Array
  def join_all(join_with = ", ", connector = "and", last_comma = false)
    return self.to_s if self.empty? || self.size==1
    connector = join_with+connector if last_comma
    [list[0...-1].join(join_with), list.last].join(connector)
  end
end

#2


6  

class Array
  def to_sentence
    sentence = self[0..-2].join(", ") + ", and " + self[-1].to_s if self.size > 1
    sentence ||= self.to_s
  end
end

so

所以

[1].to_sentence
#=> "1"
[1,2,3].to_sentence
#=> "1, 2, and 3"
[].to_sentence
#=> ""

And in Rails here is to_sentence method that uses I18n locales as well

在Rails中,这里是使用I18n语言环境的to_sentence方法

#3


0  

Might be a better way, but this would work:

可能是更好的方法,但这可行:

class Array
  def to_human_string
    arr = Array.new(self)
    last = arr.pop
    arr.join(", ") + ", and " + last.to_s
  end
end

Usage:

用法:

['a','b','c'].to_human_string
=> "a, b, and c"

You could add options for the delimiter, the 'and' and the optional oxford comma.

您可以为分隔符添加选项,'和'以及可选的牛津逗号。

#1


15  

Try: [list[0...-1].join(", "), list.last].join(", and ").

尝试:[list [0 ...- 1] .join(“,”),list.last] .join(“,and”)。

Edit: Rails has the method you were probably looking for, called to_sentence.

编辑:Rails有你可能正在寻找的方法,称为to_sentence。

In case you do not have Rails or do not wish to depend on Rails, open Array class and include the above method, like:

如果您没有Rails或者不希望依赖Rails,请打开Array类并包含上述方法,如:

class Array
  def join_all(join_with = ", ", connector = "and", last_comma = false)
    return self.to_s if self.empty? || self.size==1
    connector = join_with+connector if last_comma
    [list[0...-1].join(join_with), list.last].join(connector)
  end
end

#2


6  

class Array
  def to_sentence
    sentence = self[0..-2].join(", ") + ", and " + self[-1].to_s if self.size > 1
    sentence ||= self.to_s
  end
end

so

所以

[1].to_sentence
#=> "1"
[1,2,3].to_sentence
#=> "1, 2, and 3"
[].to_sentence
#=> ""

And in Rails here is to_sentence method that uses I18n locales as well

在Rails中,这里是使用I18n语言环境的to_sentence方法

#3


0  

Might be a better way, but this would work:

可能是更好的方法,但这可行:

class Array
  def to_human_string
    arr = Array.new(self)
    last = arr.pop
    arr.join(", ") + ", and " + last.to_s
  end
end

Usage:

用法:

['a','b','c'].to_human_string
=> "a, b, and c"

You could add options for the delimiter, the 'and' and the optional oxford comma.

您可以为分隔符添加选项,'和'以及可选的牛津逗号。