“在 Ruby 中,做同一件事有不止一种方法” - 这是什么意思?

发布于 2024-11-16 09:22:51 字数 150 浏览 2 评论 0原文

如果该主题已被讨论或非常明显,请随意删除。我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随波逐流 2024-11-23 09:22:51

“做某事的不止一种方式”意味着可以选择按照你想要的方式做某事。这样,无论您来自什么背景,您都可以使用各种编程风格。


使用 for 与块进行迭代

您可以迭代类似这样的数组。这是非常基本的,如果您有 Java 背景,这感觉很自然。

for something in an_array
   print something
end

一种更像 Ruby 的方式如下:

an_array.each do |something|
    print something
end

第一种是众所周知的做事方式。第二个是使用 ,这是一个非常强大的概念,您可以在许多 Ruby 习惯用法中找到它。基本上,数组知道如何迭代其内容,因此您可以修改它并添加类似的内容:

an_array.each_with_index do |something, index|
    print "At #{index}, there is #{something}"
end

您也可以这样做,但现在您看到上面的看起来更容易:

index = 0
for something in an_array
    print "At #{index}, there is #{something}"
    index += 1
end

照常传递参数或使用 Hashes

通常,您会像这样传递参数:

def foo(arg1, arg2, arg3)
    print "I have three arguments, which are #{arg1}, #{arg2} and #{arg3}"
end

foo("very", "easy", "classic")

=> "I have three arguments, which are very easy and classic"

但是,您也可以使用 Hash 来做到这一点:

def foo(args)
    print "I have multiple arguments, they are #{args[:arg1]}, #{args[:arg2]} and #{args[:arg3]}"
end

foo :arg1 => "in a", :arg2 => "hash", :arg3 => "cool"

=> "I have three arguments, which are in a hash and cool"

第二种形式是 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. blocks

You 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.

for something in an_array
   print something
end

A more Ruby-like way would be the following:

an_array.each do |something|
    print something
end

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:

an_array.each_with_index do |something, index|
    print "At #{index}, there is #{something}"
end

You could have done it like this too, but now you see that the above one looks easier:

index = 0
for something in an_array
    print "At #{index}, there is #{something}"
    index += 1
end

Passing arguments as usual or using Hashes

Normally, you would pass arguments like so:

def foo(arg1, arg2, arg3)
    print "I have three arguments, which are #{arg1}, #{arg2} and #{arg3}"
end

foo("very", "easy", "classic")

=> "I have three arguments, which are very easy and classic"

However, you may also use a Hash to do that:

def foo(args)
    print "I have multiple arguments, they are #{args[:arg1]}, #{args[:arg2]} and #{args[:arg3]}"
end

foo :arg1 => "in a", :arg2 => "hash", :arg3 => "cool"

=> "I have three arguments, which are in a hash and cool"

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.

土豪我们做朋友吧 2024-11-23 09:22:51

这意味着很多混乱风格战争,以及由于 细微差别,一切都以选择自由的名义。

It means a lot of confusion, style wars, and bugs due to subtle differences, all in the name of freedom of choice.

春风十里 2024-11-23 09:22:51

一个有点简单的例子是使用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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文