Ruby 将返回值传递给另一个方法

Ruby 将返回值传递给另一个方法,ruby,Ruby,我从一系列字母开始: letters = %w[c s t p b l f g d m y o u i h t r a e l o t l a e m r s n i m a y l p x s e k d] 传递它们,查找返回如下数组的所有组合[“cstp”、“cstb”、“cstl”],这是一个简短的示例 def combinations(letters) combos = letters.combi

我从一系列字母开始:

letters = %w[c s t p b l f g d m  
             y o u i h t r a e l 
             o t l a e m r s n i 
             m a y l p x s e k d]
传递它们,查找返回如下数组的所有组合
[“cstp”、“cstb”、“cstl”]
,这是一个简短的示例

def combinations(letters)
  combos = letters.combination(4) 
  combos.collect do |letter_set|
    letter_set.join(",").gsub("," ,"")  
  end
end
我试图找出如何将
组合的返回值传递到
开始字母c
。我必须通过像
和block
这样的块吗?我试过各种各样的方法,不断地说错误的论点

def start_with_letter_c(pass the return value)
  combinations.select {|word| word.match(/^ca/) }
end

给你,没有错误:

letters = %w[c s t p b l f g d m  
             y o u i h t r a e l 
             o t l a e m r s n i 
             m a y l p x s e k d]

def combinations(letters)
  combos = letters.combination(4) 
  combos.collect do |letter_set|
    letter_set.join(",").gsub("," ,"")  
  end
end

def start_with_letter_c(combinations)
  combinations.select {|word| word.match(/^ca/) }
end

start_with_letter_c(combinations(letters))
# => ["cael", "caeo", "caet", "cael", "ca ...and so on

我会这样写:

letters = %w[c s t p b l f g d m  
             y o u i h t r a e l 
             o t l a e m r s n i 
             m a y l p x s e k d]

def combinations(letters)
  letters.combination(4).map(&:join) 
end

def start_with_letter_c(combinations)
  combinations.select { |word| word.start_with?('ca') }
end

start_with_letter_c(combinations(letters))

问题是什么还不清楚;你有一个可以传递的值,为什么不能传递?您如何调用
start\u with_letter\u c
?我喜欢您使用
start\u with?
的方式,因为它比正则表达式方法更容易理解其含义。@HunterStevens:
start\u with?
方法比正则表达式版本更快,作为奖励…您不需要
组合=
。谢谢!我打电话的时候没有传信。