编程与软件工程softwaredoug.com
Ruby 与 Python 的差异,归结于 for 循环
Ruby vs. Python comes down to the for loop
2021 — 2024,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇文章对比了 Ruby 和 Python 在迭代处理上的根本差异:Python 以 for 循环为核心,把数据传回循环处理;Ruby 则通过 each 方法和代码块,把处理代码传给数据。作者用自定义集合类的示例展开分析,评论者指出文中 Ruby 示例并非惯用写法,但核心观点基本成立。
为什么经久不衰:它抓住了两种语言设计哲学的一个关键分野,对理解迭代器、代码块和语言风格差异仍有启发。
HN 评论精选
作者明显是 Python 视角,Ruby 示例不地道,但核心论点成立
这肯定是个 Pythonista 写的!作为 Rubyist,我写 Python 也一定会写错一些东西(真正同时精通两者的人很少)。Ruby 中 for 的标准替代确实是 each 和代码块,但绝不是在类里定义自定义 each 方法——那是大多数人很少做的特殊操作,更别说用 for 来实现它,太怪了。不过核心观点“Python 把数据传回 for 循环,Ruby 把代码传给数据”基本准确。代码块——语言对干净地传递单个内联 lambda/闭包作为参数的语法支持——可能是 Ruby 最特别的东西。
jrochkind1查看英文原文
This was definitely written by a pythonist! If I tried to write it, as a rubyist, I'm sure I'd get some things about python wrong. (I find it notable how few people there are that are actually familiar with both). The standard alternative to `for` in ruby does involve `each` and blocks... but definitely doesn't involve defining a custom `each` method on your class... That is a specialty thing that most developers have probably done rarely. Let alone defining it in terms of `for`, which is just weird! But the basic principle stated "Instead of passing data back to the for loop (Python) you pass the code to the data (Ruby)" -- is more or less accurate. blocks -- syntactic support in the language for cleanly passing a single in-line defined lambda/closure object as an argument -- are possibly the thing that are most special to ruby. > Python builds on for-like constructs for all kinds of processing; Ruby pushes other kinds of data processing work to methods. OK, maybe, although not totally sure what you mean. > Ruby keeps going with its methods-first approach, except instead of each we have a new set of methods commonly implemented on collections, as below: Um. I'm not sure where the author is getting this. It is certainly possible, as shown, but definitely not common to implement `select` or `map` or other methods provided by Enumerable directly on your custom class. It is a bit more common to implement `each` alone and let the `Enumerable` mixin provide the rest. But even that I'm not sure how "common" I'd call it. > Ruby, however, inverts this. Ruby puts object-orientation as the foundation of the pyramid. Ruby contains the messy procedural world in blocks, letting objects work with those procedural blocks. OK, true. The author is getting the details wrong, but I guess their overall picture is still right?
Ruby 的 for 只是 each 的语法糖,很少使用,用它实现 each 尤其奇怪
虽然大家指出这个写法不地道,但没人指出最深的原因:与很多语言不同,Ruby 的 for 不是底层基础构造,而是语法糖——for x in y 只是 y.each do |x| ... end 的糖。而且长期以来 for...in 还有运行时开销,更慢。for...in 很少被使用(它既不更简洁也不更能表达意图,只是对非 Ruby 背景的人更熟悉),主要出现在消费集合的脚本里。如果你深入到要为自定义集合实现 #each,那是个用 for 的奇怪场合。
dragonwriter查看英文原文
While people have pointed out that this is not idiomatic: class Stuff def initialize @a_list = [1, 2, 3, 4] end def each for item in @a_list yield item end end end I haven't seen anyone point out the deepest reason why: unlike in many languages, “for” is not a basic, low-level construct in Ruby, it is syntax sugar. That is: for x in y x.stuff more_stuff end is sugar for: y.each do |x| x.stuff more_stuff end (Also, for quite a long time, for...in also had runtime overhead , so it was slower syntactic sugar.) for...in is rarely used (it is syntactic sugar that is neither more concise bor more specific in intent, but more familiar to people with experience in non-Ruby languages), and mostly on scripts that consume but don’t implement collections. If you are getting deep enough to be implementing an #each method for a custom collection, that's an odd place to use for.
Python 胜在显式性,Ruby 的可组合迭代令人羡慕,Rust 采用拉式迭代
对我来说,Python 的杀手锏是显式性:我能看到一个名字,对应到 import,再对应到包;取函数指针也完全符合预期。借用 Krister Stendahl 的宗教对话法则来说,Ruby 的可组合迭代是我“神圣的嫉妒”,尤其在 Rust 里习惯之后。我用 Python 生成器很多次,最后还是不得不切换到显式 for 循环。可组合迭代模型总体更好,但偶尔我也会在 Rust 里退回显式循环。与 Ruby 不同,Rust 像 Python 一样是拉式迭代,不过也有 Ruby 式推式迭代的实验。
epage查看英文原文
For me, its the explicitness in Python that is killer. I can see a name and match that to an import and match that to a package. Grabbing a function pointer works exactly as I would expect. Stealing from Krister Stendahl's laws for religious discourse, Ruby's composable iteration is one area I have holy envy, particularly after I got used to it in Rust. I've used Python's generators many times just to have to switch to an explicit for loop. Things are generally better with a composable iteration model but occasionally I find myself switching to explicit loops in Rust. Unlike Ruby, Rust does pull-iteration like Python, though there are experiments with Ruby-style push-iteration [0] [1]. [0] https://github.com/AndWass/pushgen [1] https://epage.github.io/blog/2021/07/pushgen-experiment/
文章遗漏了 Ruby 的 Enumerable 模块这一关键概念
关于 Ruby 的讨论应该提一下 Enumerable 模块。
DFHippie查看英文原文
The discussion of Ruby could use a mention of Enumerable. https://ruby-doc.org/core-3.0.2/Enumerable.html
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选