将 "s" 添加到数组中除给定数组中的第二个元素之外的每个单词的末尾,仅使用一行代码

Add "s" to the end of each word in an array except for the 2nd element in the given array, only using one line of code

我有一个单字串数组,我想在每个单字串的末尾添加一个 's',除了数组中的第二个字符串(元素)。我可以使用 9 行代码轻松完成此操作,但更愿意使用 3 行代码来完成。

这是我使用 9 行的工作代码。

def add_s(array)
    array.each_with_index.collect do |element, index|
        if index == 1
            element
        else element[element.length] = "s"
            element
        end
    end
end

这是我只尝试使用 3 行时损坏的代码。

def add_s(array)
    array.each_with_index.map {|element, index| index == 1 ? element : element[element.length] = "s"}
end

以上将return...

array = ["hand", "feet", "knee", "table"]
add_s(array) => ["s", "feet", "s", "s"]

我正在尝试获得...

array = ["hand", "feet", "knee", "table"]
add_s(array) => ["hands", "feet", "knees", "tables"]
["hand", "feet", "knee", "table"].map.with_index{|v,i| i==1 ? v : v + 's'}
#=> ["hands", "feet", "knees", "tables"]

map.with_index 有帮助。
基本上 map.with_index 等于 each_with_index.collect.
eachcollect(与 map 相同)是多余的。

如果想改变原始数组,可以将+改为<<,但不推荐。

你应该清楚地区分方法 mutating receiver(调用它们的变量)和 pure 没有副作用的方法。另外,如果你要使用方法的结果,你应该关心方法 returns 是什么。

这里是所有索引的方法(但是1)returns "s"因为它是块returns:

foo = "bar"
foo[foo.length] = "s"
#⇒ "s"

如果您随后检查 mutated 数组,您会看到它已成功修改为您想要的内容。

input = %w[hand feet knee table]
def add_s(input)
  input.each_with_index.map do |element, index|
    index == 1 ? element : element[element.length] = "s"
  end
  input # ⇐ HERE :: return the mutated object
end
#⇒ ["hands", "feet", "knees", "tables"]

甚至更简单,不要 map,只需迭代和变异:

input = %w[hand feet knee table]
def add_s(input)
  input.each_with_index do |element, index|
    element[element.length] = "s" unless index == 1
  end
end

不是就地改变数组,首选的解决方案是 return 修改版本。为此,您应该 return 来自块的新值:

def add_s(input)
  input.each_with_index.map do |element, index|
    index == 1 ? element : element + "s"
  end
end
#⇒ ["hands", "feet", "knees", "tables"]

如果给我这样的任务,我也会维护一个要跳过的元素列表,因为迟早会有多个:

input = %w[hand feet knee scissors table]
to_skip = [1, 3]
def add_s(input)
  input.each_with_index.map do |element, index|
    next element if to_skip.include?(index)
    element + "s"
  end
end
#⇒ ["hands", "feet", "knees", "scissors", "tables"]

使用 'active_support'

很容易做到这一点
arr = ["hand", "feet", "knee", "table"]

2.0.0-p648 :023 > require 'active_support/inflector'
 => true

arr.map { |a| arr.index(a) == 1 ? a : a.pluralize }
 => ["hands", "feet", "knees", "tables"]

我建议按照提供的话来实现,

arr = ['apple', 'knee', 'crab', 'jails']
arr.each_with_index.map { |x,i| i == 1  ? x : x + 's' }
=> ["apples", "knee", "crabs", "jailss"]

但为了避免字符串末尾不需要的额外 's',

arr = ['apple', 'knee', 'crab', 'jails']
arr.each_with_index.map { |x,i| i == 1 || x[-1] == 's' ? x : x + 's' }
=> ["apples", "knee", "crabs", "jails"]

Ruby 中的换行符是完全可选的,它们总是可以用关键字、分号替换,有时甚至可以完全删除。因此,在一行中编写任何 Ruby 程序总是很容易的,无论多么复杂。

这是您的一行代码:

def add_s(array) array.each_with_index.collect do |element, index| if index == 1 then element else element[element.length] = "s"; element end end end

但是,我不完全确定为什么要将代码放在一行中,因为您的原始版本更具可读性。