1
A
回答
11
list = %w(a b c)
# there's a for statement but nobody likes it :P
for item in list
puts item
end
# so you use the each method with a block instead
# one-liner block
list.each { |item| puts item }
# multi-line block
list.each do |item|
puts item
end
4
foo = [3, 6, 3]
foo.each do |a|
puts a
end
2
foo = [1, 2, 3]
foo.each do |x|
puts x
end
1
你已经有两个关于正确答案 “为” -loop。但究竟你的榜样,我将使用:
puts foo
您也可以在这种情况下使用该看跌期权功能:
puts array.map { |i| ...some code...; x }
代替
array.each { |i| ...some code...; puts x }
例如,如果你只需要拨一次即可。
相关问题
- 1. 在Ruby中做什么==?
- 2. “|| =”在Ruby 1.9.2中做什么?
- 3. set_if_nil.call在ruby中做什么?
- 4. 如何为TObjectList做些什么?
- 5. 在SceneKit中做些什么
- 6. 如何在applicationDidFinishLaunching之前做些什么
- 7. 如何在Javascript中为“... for ...”循环做些什么?
- 8. Ruby代码在做什么?
- 9. 哪些测试工具在Ruby中做什么?
- 10. [[]] * 2在Python中做什么?
- 11. //在Python中做什么?
- 12. Python:为类的任何方法做些什么?
- 13. 终止python程序时做些什么
- 14. '|| ='操作符在ruby中做了什么?
- 15. String(42)在Ruby中做什么?
- 16. World()方法在ruby中做了什么?
- 17. 如何让我的UISwitch做些什么?
- 18. 如何设置此类做些什么?
- 19. 我能做些什么,以避免使用的foreach
- 20. <运算符在python中做了些什么?
- 21. lockflags在direct3d中做些什么
- 22. 在JavaScript/node.js中做了些什么?
- 23. 这些在maven pom.xml中做了什么
- 24. 为什么在这些git命令后什么也不做?
- 25. ruby_executable_hooks是做什么的?
- 26. Ruby的Array#shift做什么?
- 27. 如何在jquery droppable事件中做些什么
- 28. 我应该在C++中为C中的代码做些什么?
- 29. python operator = - 做什么?
- 30. 我应该如何为这个makefile做些什么?
+1问如何在标志中移动乌龟! – 2010-08-23 23:29:30