- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我刚刚开始使用 Ruby,我在 Bloc 的类(class)中已经走了很长一段路,但现在我被困在与 yield 和 blocks 有关的练习上(我发现这是迄今为止最难掌握的概念说到学习 ruby)。
以下是纯格式文本所需的规范:
以下是需要满足的 RSpecs:
describe "new_map" do
it "should not call map or map!" do
a = [1, 2, 3]
a.stub(:map) { '' }
a.stub(:map!) { '' }
expect( new_map(a) { |i| i + 1 } ).to eq([2, 3, 4])
end
it "should map any object" do
a = [1, "two", :three]
expect( new_map(a) { |i| i.class } ).to eq([Fixnum, String, Symbol])
end
end
这是他们给我的原始 def 方法:
def new_map(array)
new_array = []
array.each do |item|
# invoke the block, and add its return value to the new array
end
end
然后这是我当前的代码(更新):
def new_map(a)
new_array = []
a.each do |item|
# invoke the block, and add its return value to the new array.
yield(item, new_array)
end
end
a = [2, 3, 4]
new_map(a) do |i, e|
e << i
end
最后,当我提交我刚刚列出的当前代码时,我收到以下错误(已更新):
new_map should not call map or map! (INCOMPLETE)
expected: [2, 3, 4]
got: [1, 2, 3]
(compared using ==)
exercise_spec.rb:9:in `block (2 levels) in <top (required)>'
new_map should map any object (INCOMPLETE)
expected: [Fixnum, String, Symbol]
got: [1, "two", :three]
(compared using ==)
exercise_spec.rb:14:in `block (2 levels) in <top (required)>'
最佳答案
您没有意识到 yield 可以返回一个值。 block 中最后执行的语句是返回值。
因此您可以从每个 yield 调用中获取结果并将其添加到结果数组中。
然后,将生成的数组作为 new_map
方法的返回值。
def new_map(a)
new_array = []
a.each do |item|
# invoke the block, and add its return value to the new array.
new_array << yield(item)
end
new_array
end
关于ruby-on-rails - ruby block (产量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30132181/
使用 Python 3.4,我在此处得到 SyntaxError: invalid syntax: >>> xlist = [1,2,3,4,5] >>> [yield(x) for x in xli
在这篇文章 ( http://blogs.msdn.com/oldnewthing/archive/2008/08/13/8854601.aspx ) 中,有一个关于迭代器的流行问题和一个关于极端情况
在 Ruby 中,yield 关键字用于让出执行 block 的闭包。 这个关键字在 Python 语言中有何不同? 最佳答案 在 ruby 中,yield 是用于调用匿名函数的快捷方式。 Rub
这个问题在这里已经有了答案: check if function is a generator (13 个答案) 关闭 7 年前。 检查函数是否为生成器的可靠方法是什么,例如: let fn = f
jquery 中是否可以产生变量?我想在部分 View 中设置一个变量,根据该变量我将某个菜单项设置为事件状态。 我有一个菜单,我想在其中产生像这样的事件项目(在我的主布局 View 中): func
问题 是否可以将 yielding pytest fixtures(用于设置和拆卸)作为参数传递给测试函数? 上下文 我正在测试一个对象,该对象从单个目录中的文件读取数据/向文件写入数据。该目录的路径
我刚刚开始使用 Ruby,我在 Bloc 的类(class)中已经走了很长一段路,但现在我被困在与 yield 和 blocks 有关的练习上(我发现这是迄今为止最难掌握的概念说到学习 ruby
我一直在寻找这一点,但我还没有得到任何关于它的信息!。 我看了很多关于“ yield 返回”的视频和红色文章,我想我对它有更好的理解,但有一点我无法理解 我应该使用 yield return 的正确接
UPDATE(反映最先进的知识水平)状态:2017-05-12 进行此更新的原因是,当我问这个问题时,我并不知道我发现了有关Python3如何“在幕后”工作的信息。 从所有得出的结论是: 如果您为迭代
我正在尝试在 Atmel Studio 7 中为 Arduino 编写代码。为了使其与 Arduino IDE 相似,我正在尝试调整其库。 但是我已经被 delay() 阻止了,它使用了 yield(
我正在构建一个简单的 C++ 服务器,我在其中通过 POST 请求接收图像,将其发送到 GPU 进行处理,一旦我从 GPU 获得结果,就发回响应。 为了能够处理许多同时连接(并学习新的东西),我正在使
我想生成从每片叶子到树根的所有路径。我想用生成器来做,以节省内存(树可以很大)。这是我的代码: def paths(self, acc=[]): if self.is_leaf():
假设我有以下代码(缩小上下文以限制问题范围) public static IEnumerable GetThemColors(){ var ids = GetThePrimaryIds();
我想我也有同样的问题。 Using multiple yields to insert content 我尝试了这个解决方案。我试过 在我的 application.html.erb 中有 conte
我熟悉 yield 以返回值,这主要归功于 this question 但是当它在赋值的右边时,yield 会做什么呢? @coroutine def protocol(target=None):
function * d1 (p) { p-=1; yield p; p-=2; yield p; } var g=d1 (9); var h; console.l
以下代码在 golang 中实现了 yield 模式。作为一项实验,我正在实现一个all permutations 生成器。但是,当我将 slice A 返回到 channel 时,如果我不创建数组的
Node.js 现在有生成器。 我的理解是,生成器可用于编写看起来更加线性的代码,并避免回调 hell 和末日风格编码的金字塔。 所以到目前为止,我的理解是,在生成器内部,代码会一直执行,直到它到达“
我遇到的问题真的很奇怪。由于某种原因,我的 catch block 中的 put 没有在下面执行。这是我的传奇: function* postLoginFormSaga(action) { l
假设我只想快速使以下方法异步运行: ResultType SynchronousCode(ParamType x) { return SomeLongRunningWebRequest(x)
我是一名优秀的程序员,十分优秀!