我无法理解 ruby 中的 Blocks 与 Procs。我的基本想法是,proc 是一种保存为对象的方法,您可以重复调用它,而不必一遍又一遍地继续编写相同的代码行。
我的麻烦在于在方法中接受块作为参数。
家庭作业问题超级简单。
编写一个方法,接受一个块作为参数并反转字符串中的所有单词。
以下是他们正在寻找的答案。
def reverser(&prc)
sentence = prc.call
words = sentence.split(" ")
words.map { |word| word.reverse }.join(" ")
end
我有两个问题——
1你如何调用这个方法因为如果我把
print reverser("Hello")
我收到错误“参数数量错误(给定 1,预期为 0)”
其次,为什么不直接写下面的方法呢?编写一个接受块的方法有什么好处?
def reverser(string)
string.split.map{|x| x.reverse}.join(" ")
end
你会这样称呼它:
print reverser { "Hello" }
或者,如果您的块有几行长,那么像这样:
print reverser do
"Hello"
end
地图就是一个很好的例子。map 的作用是获取一个数组并根据您需要的任何规则映射(转换)每个元素。
这些映射规则必须是代码。这意味着,它不能是字符串或数字之类的,它必须是一个函数。Block 在这里用作函数,您可以使用最少的代码编写它。
所以在你的例子中你有这个:
words.map { |word| word.reverse }.join(" ")
如果您无法将块传递给映射,那么您必须定义该函数并将其传递到某处 - 并命名该函数。那只是效率不高。
让我们更改此块以使其仅在以大写字母开头时才反转单词。
words.map do |word|
if word[0] =~ /[A-Z]/
word.reverse
else
word
end.join(" ")
如果没有块,您需要定义该函数,您在任何其他地方都不需要它并调用它。那只是效率不高。这就是它的样子
def reverse_if_starts_with_capital_letter(word)
if word[0] =~ /[A-Z]/
word.reverse
else
word
end
end
# not sure if this syntax would work, just demonstrates idea
words.map(&reverse_if_starts_with_capital_letter)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句