Ruby가 Python보다 Rails에 더 적합한 이유는 무엇입니까? [닫은]
Python과 Ruby는 일반적으로 유사한 표현력과 힘을 가진 가까운 사촌으로 간주됩니다 (역사적인 짐은 상당히 다르지만). 그러나 일부에서는 Rails 프레임 워크의 엄청난 성공이 실제로 빌드 된 언어 인 Ruby 자체와 관련이 있다고 주장했습니다. 그렇다면 왜 Ruby가 Python보다 그러한 프레임 워크에 더 적합할까요?
아마도 두 가지 주요 차이점이 있습니다.
Ruby에는 우아하고 익명의 클로저가 있습니다.
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
익명의 클로저 / 람다를 사용하면 차단할 수있는 새로운 언어 기능을 더 쉽게 에뮬레이션 할 수 있습니다. 파이썬에서는 클로저가 존재하지만 사용하려면 이름을 지정해야합니다. 따라서 클로저를 사용하여 새로운 언어 기능을 에뮬레이트하는 대신 클로저를 사용하고 있다는 사실에 대해 명시해야합니다.
Ruby는 더 깨끗하고 사용하기 쉬운 메타 프로그래밍을 제공합니다.
이것은 주로 사용하기 쉽기 때문에 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
두 경우 모두 다음을 수행 할 수 있습니다.
Bar.new.hello
"HELLO"가 인쇄됩니다. 이 class_eval
메서드는 또한 String을 사용하므로 전달 된 매개 변수에 따라 다른 의미를 갖는 클래스가 생성 될 때 즉석에서 메서드를 만들 수 있습니다.
사실 이런 종류의 메타 프로그래밍은 파이썬 (그리고 다른 언어에서도)으로 할 수 있지만, 메타 프로그래밍은 특별한 프로그래밍 스타일이 아니기 때문에 루비는 한 발 앞서 있습니다. Ruby에서는 모든 것이 객체이고 모든 코드 라인이 직접 실행된다는 사실에서 비롯됩니다. 결과적으로 Class
es는 그 자체가 객체이고 클래스 본문은 self
Class를 가리키며 클래스를 만들 때 클래스에서 메서드를 호출 할 수 있습니다.
이것은 Rails에서 가능한 선언 성의 정도와 키워드 또는 새로운 블록 언어 기능처럼 보이는 새로운 선언적 기능을 구현할 수있는 용이성에 큰 책임이 있습니다.
주장한 사람들
Rails 프레임 워크의 엄청난 성공은 실제로 빌드 된 언어와 관련이 있습니다.
are (IMO) mistaken. That success probably owes more to clever and sustained marketing than to any technical prowess. Django arguably 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!
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.
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.
Is this debate a new "vim versus emacs" debate?
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.
I can imagine that it would be the same if I were experienced with Ruby/Rails.
Both have similar philosophy and do the job in a fast and elegant way. The better choice is what you already know.
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:
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:
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.
The real answer is neither Python 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.
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.
If I had have found Python and one of its MVC frameworks first I would more than likely be praising that language instead!
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.
The argument that Rubys meta programming would make it better suited is IMO incorrect. You don't need metaprogramming for frameworks like this.
So I think we can conclude that Ruby are not better (and likely neither worse) than Python in this respect.
Because Rails is developed to take advantage of Rubys feature set.
A similarly gormless question would be "Why is Python more suitable for Django than Ruby is?".
I suppose we should not discuss the language features per se but 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.
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.
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.
I think that the syntax is cleaner and Ruby, for me at least, is just a lot more "enjoyable"- as subjective as that is!
Two answers :
a. Because rails was written for ruby.
b. For the same reason C more suitable for Linux than Ruby
All of this is TOTALLY "IMHO"
In Ruby there is ONE web-application framework, so it is the only framework that is advertised for that language.
Python has had several since inception, just to name a few: Zope, Twisted, Django, TurboGears (it itself a mix of other framework components), Pylons (a kinda-clone of the Rails framework), and so on. None of them are python-community-wide supported as "THE one to use" so all the "groundswell" is spread over several projects.
Rails has the community size solely, or at least in the vast majority, because of Rails.
Both Python and Ruby are perfectly capable of doing the job as a web applications framework. Use the one YOU (and your potential development team) like and can align on.
참고URL : https://stackoverflow.com/questions/1099305/why-is-ruby-more-suitable-for-rails-than-python
'program story' 카테고리의 다른 글
Git에서 가장 많이 변경된 파일 찾기 (0) | 2020.09.05 |
---|---|
hitTest : withEvent를 사용하여 수퍼 뷰 프레임 외부의 하위 뷰에 대한 터치 캡처 : (0) | 2020.09.05 |
두 NSDates 사이의 신속한 일 (0) | 2020.09.04 |
뒤로 버튼의 텍스트를 변경하는 방법 (0) | 2020.09.04 |
Vim : 두 번째 행마다 삭제하는 방법? (0) | 2020.09.04 |