“在 Ruby 中,做同一件事有不止一种方法” - 这是什么意思?
如果该主题已被讨论或非常明显,请随意删除。我有 C# 背景,打算学习 Ruby。我读到的所有关于它的内容似乎都很有趣。但我对 Ruby 的基本哲学“做一件事有不止一种方法”感到困惑。有人可以提供 2 或 3 个简单的算术或字符串示例来阐明这一点,例如语法或逻辑等。
谢谢
Feel free to delete this topic if it's discussed or quite obvious. I hail from C# background and I'm planning to learn Ruby. Everything I read about it seems quite intriguing. But I'm confused over this basic philosophy of Ruby that "there's more than one way to do one thing". Can someone provide 2 or 3 simple arithmetic or string examples to make this point clear, like if its about the syntaxes or logics etc.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“做某事的不止一种方式”意味着可以选择按照你想要的方式做某事。这样,无论您来自什么背景,您都可以使用各种编程风格。
使用
for
与块进行迭代您可以迭代类似这样的数组。这是非常基本的,如果您有 Java 背景,这感觉很自然。
一种更像 Ruby 的方式如下:
第一种是众所周知的做事方式。第二个是使用 块 ,这是一个非常强大的概念,您可以在许多 Ruby 习惯用法中找到它。基本上,数组知道如何迭代其内容,因此您可以修改它并添加类似的内容:
您也可以这样做,但现在您看到上面的看起来更容易:
照常传递参数或使用
Hash
es通常,您会像这样传递参数:
但是,您也可以使用 Hash 来做到这一点:
第二种形式是 Ruby on Rails 过度使用的形式。令人高兴的是您现在已经命名了参数。当你经过它们时,你会更容易记住它们的用途。
"More than one way of doing something" means having the choice of doing something the way you want it. That way you can use various programming styles, no matter what background you're coming from.
Iteration using
for
vs. blocksYou can iterate over an array of things like so. This is pretty basic, and if you're from a Java background, this feels kind of natural.
A more Ruby-like way would be the following:
The first is a rather well known way of doing things. The second one is using blocks, a very powerful concept that you'll find in many Ruby idioms. Basically, the array knows how to iterate over its contents, so you can modify this and add something like:
You could have done it like this too, but now you see that the above one looks easier:
Passing arguments as usual or using
Hash
esNormally, you would pass arguments like so:
However, you may also use a Hash to do that:
The second form is one used excessively by Ruby on Rails. The nice thing is that you now have named parameters. When you are passing them, you will more easily remember what they are used for.
这意味着很多混乱,风格战争,以及由于 细微差别,一切都以选择自由的名义。
It means a lot of confusion, style wars, and bugs due to subtle differences, all in the name of freedom of choice.
一个有点简单的例子是使用alias/alias_method(另请注意,对于几乎相同的事情有两种类似的方法,例如alias 与alias_method)。
假设您正在一个项目中工作,并且忘记了要使用哪个 API。
该方法的名称又是什么?
好吧,您可以留在手头程序的域逻辑内,并继续按照您想要的方式使用它;那么您只需在其他程序的主入口点添加一个别名即可。
人们可以默认使用 .collect ,也可以使用 .map ,这对你个人使用的几乎没有什么影响(我使用 .map 因为它更短)。
别名的使用对我很有帮助,因为几个月后,我经常不记得如何使用某些东西。是的,我可以查一下,但为什么我还要费心呢?我可以只使用别名。 (请注意,我确实尝试使用别名和 API 尽可能保持简单。)
A somewhat trivial example is the use of alias/alias_method (also note that there are two similar ways for almost the same thing, e. g. alias versus alias_method).
Consider that you are working in a project and you forgot which API to use.
What was the name of the method again?
Well, you can just remain within the domain logic of your program at hand, and continue to work with it the way you want to; then you are going to simply add an alias in the main entry point of your other program.
People can use by default .collect or they can use .map, it makes little difference what you personally would use (I use .map since it is shorter).
The use of aliases helped me because after some months, I often can not remember how to use something. Yes, I could look it up, but why would I have to bother anyway? I can just use an alias instead. (Note that I do try to remain as simple as possible with aliases and APIs.)