组合数组中的元素(RubyMonk第6章第1课)

lyo*_*eta 4 ruby arrays map

我真的尝试过研究这个问题,但我现在已经接近它了,我担心如果不寻求帮助我就找不到解决方案.我正在浏览RubyMonk,其中一个练习让我完全陷入困境.

class Hero
  def initialize(*names)
    @names = names
  end
  def full_name
    # a hero class allows us to easily combine an arbitrary number of names
  end
end

def names
  heroes = [Hero.new("Christopher", "Alexander"),
            Hero.new("John", "McCarthy"),
            Hero.new("Emperor", "Joshua", "Abraham", "Norton")]
  # map over heroes using your new powers!
end
Run Code Online (Sandbox Code Playgroud)

您可以在评论中看到代码要求的内容; 获取英雄变量中的名称并将它们组合成一个名称.我已经尝试过测试一些put并且除了"#"或"nil"之外我无法在STDOUT中获得任何内容,所以很明显我没有正确使用它.

目标的要求说不要使用.map或.collect,但我认为你应该这样做,因为如果你不使用.map或.collect它就不符合要求.

想法?

evf*_*qcg 5

class Hero
  def initialize(*names)
    @names = names
  end
  def full_name
    @names.join(' ')
  end
end

def names
  heroes = [Hero.new("Christopher", "Alexander"),
            Hero.new("John", "McCarthy"),
            Hero.new("Emperor", "Joshua", "Abraham", "Norton")]
  heroes.map(&:full_name)
end
Run Code Online (Sandbox Code Playgroud)