为什么 Ruby 比 Python 更适合 Rails?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1099305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:29:09  来源:igfitidea点击:

Why is Ruby more suitable for Rails than Python?

pythonruby-on-railsrubyweb-frameworks

提问by Victor Yan

Python and Ruby are usually considered to be close cousins (though with quite different historical baggage) with similar expressiveness and power. But some have argued that the immense success of the Rails framework really has a great deal to do with the language it is built on: Ruby itself. So why would Ruby be more suitable for such a framework than Python?

Python 和 Ruby 通常被认为是具有相似表达能力和功能的近亲(尽管有着截然不同的历史包袱)。但是有些人认为 Rails 框架的巨大成功确实与它所基于的语言有很大关系:Ruby 本身。那么为什么 Ruby 比 Python 更适合这样的框架呢?

回答by Yehuda Katz

There are probably two major differences:

可能有两个主要区别:

Ruby has elegant, anonymous closures.

Ruby 有优雅的匿名闭包。

Rails uses them to good effect. Here's an example:

Rails 使用它们效果很好。下面是一个例子:

class WeblogController < ActionController::Base
  def index
    @posts = Post.find :all
    respond_to do |format|
      format.html
      format.xml { render :xml => @posts.to_xml }
      format.rss { render :action => "feed.rxml" }
    end
  end
end

Anonymous closures/lambdas make it easier to emulate new language features that would take blocks. In Python, closures exist, but they must be named in order to be used. So instead of being able to use closures to emulate new language features, you're forced to be explicit about the fact that you're using a closure.

匿名闭包/lambdas 使模拟需要块的新语言功能变得更容易。在 Python 中,闭包是存在的,但它们必须被命名才能使用。因此,您不能使用闭包来模拟新的语言功能,而是被迫明确说明您正在使用闭包的事实。

Ruby has cleaner, easier to use metaprogramming.

Ruby 具有更干净、更易于使用的元编程。

This is used extensively in Rails, primarily because of how easy it is to use. To be specific, in Ruby, you can execute arbitrary code in the context of the class. The following snippets are equivalent:

这在 Rails 中被广泛使用,主要是因为它很容易使用。具体来说,在 Ruby 中,您可以在类的上下文中执行任意代码。以下代码段是等效的:

class Foo
  def self.make_hello_method
    class_eval do
      def hello
        puts "HELLO"
      end
    end
  end
end

class Bar < Foo # snippet 1
  make_hello_method
end

class Bar < Foo; end # snippet 2
Bar.make_hello_method

In both cases, you can then do:

在这两种情况下,您都可以执行以下操作:

Bar.new.hello  

which will print "HELLO". The class_evalmethod also takes a String, so it's possible to create methods on the fly, as a class is being created, that have differing semantics based on the parameters that are passed in.

这将打印“你好”。该class_eval方法还接受一个字符串,因此可以在创建类时动态创建方法,这些方法根据传入的参数具有不同的语义。

It is, in fact, possible to do this sort of metaprogramming in Python (and other languages, too), but Ruby has a leg up because metaprogramming isn't a special style of programming. It flows from the fact that in Ruby, everything is an object and all lines of code are directly executed. As a result, Classes are themselves objects, class bodies have a selfpointing at the Class, and you can call methods on the class as you are creating one.

事实上,可以在 Python(以及其他语言)中进行这种元编程,但 Ruby 有优势,因为元编程不是一种特殊的编程风格。它源于这样一个事实,即在 Ruby 中,一切都是对象,所有代码行都是直接执行的。因此,Classes 本身就是对象,类体有一个self指向类的指针,并且您可以在创建类时调用类上的方法。

This is to large degree responsible for the degree of declarativeness possible in Rails, and the ease by which we are able to implement new declarative features that look like keywords or new block language features.

这在很大程度上决定了 Rails 中可能的声明性程度,以及我们能够轻松实现看起来像关键字或新块语言特性的新声明性特性。

回答by Vinay Sajip

Those who have argued that

那些争论过的人

the immense success of the Rails framework really has a great deal to do with the language it is built on

Rails 框架的巨大成功与它所基于的语言有很大关系

are (IMO) mistaken. That success probably owes more to clever and sustained marketing than to any technical prowess. Djangoarguably does a better job in many areas (e.g. the built-in kick-ass admin) without the need for any features of Ruby. I'm not dissing Ruby at all, just standing up for Python!

(IMO) 是错误的。这种成功可能更多地归功于聪明和持续的营销,而不是任何技术实力。Django可以说在很多方面做得更好(例如内置的 kick-ass 管理员),而无需 Ruby 的任何功能。我一点也不在贬低 Ruby,只是为 Python 挺身而出!

回答by Matt Briggs

The python community believes that doing things the most simple and straight forward way possible is the highest form of elegance. The ruby community believes doing things in clever ways that allow for cool code is the highest form of elegance.

Python 社区认为,尽可能以最简单直接的方式做事是优雅的最高形式。ruby 社区相信以允许酷代码的聪明方式做事是优雅的最高形式。

Rails is all about if you follow certain conventions, loads of other things magically happen for you. That jives really well with the ruby way of looking at the world, but doesn't really follow the python way.

如果你遵循某些约定,Rails 就是一切,许多其他事情会神奇地发生在你身上。这与 ruby​​ 看待世界的方式非常吻合,但并不真正遵循 python 方式。

回答by luc

Is this debate a new "vim versus emacs" debate?

这场辩论是一场新的“vim vs emacs”辩论吗?

I am a Python/Django programmer and thus far I've never found a problem in that language/framework that would lead me to switch to Ruby/Rails.

我是一名 Python/Django 程序员,到目前为止,我从未在该语言/框架中发现会导致我切换到 Ruby/Rails 的问题。

I can imagine that it would be the same if I were experienced with Ruby/Rails.

我可以想象,如果我对 Ruby/Rails 有经验,情况也会一样。

Both have similar philosophy and do the job in a fast and elegant way. The better choice is what you already know.

两者都有相似的理念,并以快速而优雅的方式完成工作。更好的选择是你已经知道的。

回答by fields

Personally, I find ruby to be superior to python in many ways that comprise what I'd call 'consistent expressiveness'. For example, in ruby, join is a method on the array object which outputs a string, so you get something like this:

就我个人而言,我发现 ruby​​ 在许多方面都优于 python,包括我所说的“一致的表现力”。例如,在 ruby​​ 中,join 是输出字符串的数组对象上的一个方法,因此您会得到如下内容:

numlist = [1,2,3,4]
#=> [1, 2, 3, 4]
numlist.join(',')
#=> "1,2,3,4"

In python, join is a method on the string object but which throws an error if you pass it something other than a string as the thing to join, so the same construct is something like:

在 python 中,join 是字符串对象上的一个方法,但是如果您将字符串以外的其他内容作为要加入的对象传递给它,则会引发错误,因此相同的构造类似于:

numlist = [1,2,3,4]
numlist
#=> [1, 2, 3, 4]
",".join([str(i) for i in numlist])
#=> '1,2,3,4'

There are a lot of these little kinds of differences that add up over time.

随着时间的推移,有很多这样的小差异会累积起来。

Also, I cannot think of a better way to introduce invisible logic errors than to make whitespace significant.

此外,我想不出比使空白变得重要的更好的方法来引入不可见的逻辑错误。

回答by Kris

The real answer is neitherPython or Ruby are better/worse candidates for a web framework. If you want objectivity you need to write some code in both and see which fits your personal preference best, including community.

真正的答案是既不Python或Ruby是一个Web框架更好/更糟糕的候选人。如果您想要客观性,您需要在两者中编写一些代码,然后看看哪个最适合您的个人喜好,包括社区。

Most people who argue for one or other have either never used the other language seriously or are 'voting' for their personal preference.

大多数支持一种或另一种语言的人要么从未认真使用过另一种语言,要么是为他们的个人偏好“投票”。

I would guess most people settle on which ever they come in to contact with first because it teaches them something new (MVC, testing, generators etc.) or does something better (plugins, templating etc). I used to develop with PHP and came in to contact with RubyOnRails. If I had have known about MVC before finding Rails I would more than likely never left PHP behind. But once I started using Ruby I enjoyed the syntax, features etc.

我猜大多数人会决定先接触哪个,因为它教会了他们一些新的东西(MVC、测试、生成器等)或者做得更好(插件、模板等)。以前用PHP开发,接触到RubyOnRails。如果我在发现 Rails 之前就知道 MVC,我很可能永远不会把 PHP 抛在后面。但是一旦我开始使用 Ruby,我就喜欢它的语法、特性等。

If I had have found Python and one of its MVC frameworks first I would more than likely be praising that language instead!

如果我首先找到了 Python 和它的一个 MVC 框架,我很可能会赞美那门语言!

回答by Lennart Regebro

Python has a whole host of Rails-like frameworks. There are so many that a joke goes that during the typical talk at PyCon at least one web framework will see the light.

Python 有一整套类似 Rails 的框架。有这么多的笑话,在 PyCon 的典型演讲中,至少有一个 Web 框架会看到曙光。

The argument that Rubys meta programming would make it better suited is IMO incorrect. You don't need metaprogramming for frameworks like this.

Rubys 元编程将使其更适合的论点是 IMO 不正确的。对于这样的框架,您不需要元编程。

So I think we can conclude that Ruby are not better (and likely neither worse) than Python in this respect.

所以我认为我们可以得出结论,Ruby 在这方面并不比 Python 好(而且可能也不差)。

回答by Paddy3118

Because Rails is developed to take advantage of Rubys feature set.

因为 Rails 是为了利用 Rubys 功能集而开发的。

A similarly gormless question would be "Why is Python more suitable for Django than Ruby is?".

一个类似的问题是“为什么 Python 比 Ruby 更适合 Django?”。

回答by Giorgi

I suppose we should not discuss the language features per sebut rather the accents the respective communities make on the language features. For example, in Python, re-opening a class is perfectly possible but it is not common; in Ruby, however, re-opening a class is something of the daily practice. this allows for a quick and straightforward customization of the framework to the current requirement and renders Ruby more favorable for Rails-like frameworks than any other dynamic language. Hence my answer: common use of re-opening classes.

我想我们不应该讨论语言特征本身,而应该讨论各个社区对语言特征的强调。例如,在Python中,重新打开一个类是完全可能的,但并不常见;然而,在 Ruby 中,重新打开一个类是日常实践的一部分。这允许根据当前需求快速直接地自定义框架,并使 Ruby 比任何其他动态语言更适合类 Rails 框架。因此我的回答是:重新开放课程的常见用途。

回答by Faisal Vali

Some have said that the type of metaprogramming required to make ActiveRecord (a key component of rails) possible is easier and more natural to do in ruby than in python - I do not know python yet;), so i cannot personally confirm this statement.

有人说,使 ActiveRecord(rails 的一个关键组件)成为可能所需的元编程类型在 ruby​​ 中比在 python 中更容易、更自然——我还不知道 python;),所以我不能亲自确认这个说法。

I have used rails briefly, and its use of catchalls/interceptors and dynamic evaluation/code injection does allow you to operate at a much higher level of abstraction than some of the other frameworks (before its time). I have little to no experience with Python's framework - but i've heard it's equally capable - and that the python community does a great job supporting and fostering pythonic endeavors.

我曾简单地使用过 rails,它使用的 catchalls/interceptors 和动态评估/代码注入确实允许您在比其他一些框架(在其时间之前)更高的抽象级别上运行。我几乎没有使用 Python 框架的经验——但我听说它同样有能力——而且 Python 社区在支持和促进 Pythonic 的努力方面做得很好。