这是我的代码:
class Array
def anotherMap
self.map {yield}
end
end
print [1,2,3].anotherMap{|x| x}
Run Code Online (Sandbox Code Playgroud)
我期望得到[1,2,3]的输出,但我得到[nil,nil,nil]
我的代码出了什么问题?
您的代码不会产生您传递给的块所产生的值#map.您需要提供一个块参数并yield使用该参数调用:
class Array
def anotherMap
self.map {|e| yield e }
end
end
print [1,2,3].anotherMap{|x| x}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6341 次 |
| 最近记录: |