从另一个数组填充数组

时间:2022-08-18 21:17:43

I would like to create an array with the first letter from each element, but I keep just getting the entire element - what am I doing wrong?

我想用每个元素的第一个字母创建一个数组,但是我一直在获取整个元素——我做错了什么?

def each_group_by_first_letter
  self.each do |x| 
    first_letter = []
    first_letter = x[0, 1].to_s
  end

  x = ["abcd", "efgh", "able"]
  x.each_group_by_first_letter do |letter, words|
    printf("%s: %s\n", letter, words)
  end

2 个解决方案

#1


8  

There are several problems with your code. Among them:

您的代码有几个问题。其中包括:

  • You create an array called first_letter, but then overwrite it with a string on the next line instead of adding the string to it. (To add an item to an array you will usually use Array#push or Array#<<.)
  • 您创建了一个名为first_letter的数组,但随后使用下一行的字符串覆盖它,而不是将字符串添加到其中。(要向数组添加项,通常使用数组#push或数组#<。)
  • You don't return first_letter, which means you're implicitly returning the array itself (assuming that's what self is--because that's what Array#each returns).
  • 不返回first_letter,这意味着隐式地返回数组本身(假设这是self——因为每个数组都返回#)。
  • When you call each_group_by_first_letter you pass it a block (do ...) but your method doesn't take or use a block. You probably mean to call each on the result of each_group_by_first_letter.
  • 当您调用each_group_by_first_letter时,您会传递一个块(do…),但是您的方法不会接收或使用一个块。您可能想要对each_group_by_first_letter的结果进行调用。

Regardless, the Array class already has the tools you need--no need to define a new method for this.

不管怎样,数组类已经有了您需要的工具——不需要为此定义一个新方法。

x = [ 'abcd', 'efgh', 'able' ]

x.map {|word| word[0] }
# => [ 'a', 'e', 'a' ]

#2


3  

x = ["abcd", "efgh", "able"]
y = x.map{|e| e[0]}          # keeps x intact

or

x = ["abcd", "efgh", "able"]
x.map!{|e| e[0]}             # modifies x

 => ["a", "e", "a"] 

#1


8  

There are several problems with your code. Among them:

您的代码有几个问题。其中包括:

  • You create an array called first_letter, but then overwrite it with a string on the next line instead of adding the string to it. (To add an item to an array you will usually use Array#push or Array#<<.)
  • 您创建了一个名为first_letter的数组,但随后使用下一行的字符串覆盖它,而不是将字符串添加到其中。(要向数组添加项,通常使用数组#push或数组#<。)
  • You don't return first_letter, which means you're implicitly returning the array itself (assuming that's what self is--because that's what Array#each returns).
  • 不返回first_letter,这意味着隐式地返回数组本身(假设这是self——因为每个数组都返回#)。
  • When you call each_group_by_first_letter you pass it a block (do ...) but your method doesn't take or use a block. You probably mean to call each on the result of each_group_by_first_letter.
  • 当您调用each_group_by_first_letter时,您会传递一个块(do…),但是您的方法不会接收或使用一个块。您可能想要对each_group_by_first_letter的结果进行调用。

Regardless, the Array class already has the tools you need--no need to define a new method for this.

不管怎样,数组类已经有了您需要的工具——不需要为此定义一个新方法。

x = [ 'abcd', 'efgh', 'able' ]

x.map {|word| word[0] }
# => [ 'a', 'e', 'a' ]

#2


3  

x = ["abcd", "efgh", "able"]
y = x.map{|e| e[0]}          # keeps x intact

or

x = ["abcd", "efgh", "able"]
x.map!{|e| e[0]}             # modifies x

 => ["a", "e", "a"]