在Ruby中创建一个接收数组并返回字符串的方法

时间:2016-11-11 20:57:32

标签: ruby-on-rails ruby

你如何在Ruby中解决这个问题。 “定义一个名为yeller的方法,它接受一个字符数组并返回一个带有ALLCAPS版本输入的字符串。确认yeller(['o','l','d'])返回”OLD“。 .join,.map,.upcase方法。“

到目前为止,我有:

def yeller(x) 
  x.map do |y|  
  y.upcase.join     
    puts y
  end
end
yeller(['o', 'l', 'd'])

3 个答案:

答案 0 :(得分:6)

这很简单

def yeller(x)
  x.join.upcase
end

yeller(['o', 'l', 'd'])
 => "OLD" 

join使您的角色列表成为字符串,upcase使该字符串成为大写

答案 1 :(得分:1)

这是您从中获得问题的教科书的答案。

def yeller(chars)
  chars.map(&:upcase).join
end
yeller(['o', 'l', 'd'])
=> "OLD"

答案 2 :(得分:-1)

试试这个

def yeller(x)
  imsupercool = x.map do |y|
    y.upcase
  end
   imsupercool.join
end
puts yeller(['o', 'l', 'd'])
相关问题