Kylexlau's Weblog

怎样学Ruby和Rails

为什么对Ruby感兴趣

最近我对Ruby产生了浓厚兴趣。之前学过一段时间的Python,感觉Python虽然用途比Ruby更广泛,却不适合我。

Python的哲学是:

Python的创造者认为一定有一种最佳的方法去完成某件事。这种哲学有点过于死板,也不符合现实。

Perl的格言是:

Larry Wall认为可以用许多不同的方法,去完成同一件事情。显然,我是更认同Perl的哲学的。

Ruby的格言是,程序员之友(A Programmer’s Best Friend)。Ruby是以人为中心,为程序员的方便而创造的编程语言。

Ruby继承了很多Perl的精华和哲学。所以,相对Python,我更喜欢Ruby。

别人是怎么学Ruby的

不同的人有不同的背景,有不同的学习方式。

这里有一大堆Rails社区中的领军人物说他们是怎么学会Ruby和Rails的:

  • DHH: I learned Ruby by programming in anger. Attempting to make something real. Not just a toy program.
  • David Black: I learned ruby via pickaxe and lots of practice and both reading and answering questions on ruby-talk.
  • Evan Phoenix: reading code WHILE writing code
  • Yehuda Katz: I tried impossibly hard things to force myself to learn.
  • Laurent Sansonetti: I learned ruby via pickaxe, reading the posts on ruby-talk and then reading MRI’s source code
  • Ninh Bui: I was quite a java fanboy back, writing struts + j2ee enterprise apps, Hongli forced me to look at Ruby over a weekend and I learned the Ruby basics. I then learned Rails by googling, reading books and source code.
  • Tim Connor: the rails community habit of obsessively blogging was probably the biggest help
  • Lar Van Der Jagt: Ryan Bates’s railscasts for sure. The Rails Way once I knew enough to dig deeper. What I wish I’d done tho is work with experts!
  • Arun Gupta: The Rails guides and Agile Development with Rails book.
  • Geoffrey Grosenbach: After I had read a few tutorials, I started by spending a few hours reading through the API docs. Even if you don’t understand everything, it’s a fantastic way to become familiar with what’s available.
  • Nate Todd: I learned MVC with a few CakePHP apps first. It helped me learn best practices in a language I was familiar with.
  • Chris Wanstrath: I learned Ruby on Rails by writing apps and reading the framework’s source.

从他们的经验里,我学到的是,最佳的学习方式是自己写实用的代码,其次是读别人写的好代码,再其次是读经典的好书。

别人的建议

  • Bob Martens: Get involved in the community in some way. They know more than you. ;)
  • Ismael Celis: understanding the relationship between the parts of MVC. Loads of people coming to Rails don’t know what a design pattern is.
  • @jeromegn: I find the best trick to learn RoR is to simply try building something. Rails docs and learning Ruby in parallel helped me the most
  • @johnbender: knowing why instance vars are available in templates, etc. Essentially knowing the basics of ruby would be my initial advice
  • @ryandotsmith: writers read. Find popular projects on github (i.e. radiant ) and study their specs.
  • Sunil Karkera: understanding MVC in Rails was the most important thing for me when I started.
  • Luke Burton: basic screencasts showing something impressive being achieved in small amounts of code were a great start
  • DHH: Pick a real project and program in anger. That’s how I’d recommend learning any language including Rails and Ruby.
  • Anthony Green: Accept that there’s a Rails Way and you need to learn it. What helped me most ? - the community.
  • Kent Fenwick: Learn by doing a REAL project. Pick something you want to build and slowly chip away at it.
  • Trevor Turk: learn by reading and writing codes meaning you should try to build something you want instead of dumbly following tutorials.
  • Ryan Bates: Rails is made up of many technologies (HTML, CSS, Ruby, OOP, SQL, etc.). If you’re struggling with Rails, focus on weakest part.
  • Geoffrey Grosenbach: And there are many examples throughout that will help a new developer get a feel for the philosophy of Rails. Many people have learned from the Rails from Scratch series at PeepCode
  • @eifon: A good grasp of MVC is invaluable as is knowing some Ruby before you start.
  • John Yerhot: don’t be afraid to ask questions and use support channels - irc, mail list, rails bridge, etc.
  • Roy Wright: use the same version of rails as is in the book you are using.
  • @brianthecoder: read other people’s code
  • Ninh Bui: I learned by having discussions with a lot of people, I can definitely recommend that
  • Chris Wanstrath: Stop asking other people for advice and start coding something.

最后一条建议是,不要去寻求别人的建议了,现在就开始写点代码吧。所以说,编程这件事,终极建议仍然是,写代码。代码写的越多,编程水平越高。

读完这一堆的建议,我得到的感悟是:

  • 自己写代码最重要。
  • 读别人的代码其次。
  • 理解MVC是什么也很重要。

到哪里去找别人的代码读,Github是最佳场所。注意别人是怎么解决你所解决不了的问题的。当然,先要自己写点代码才会有解决不了的问题。

How Person 和 Why Person

  • How Person: 喜欢问要怎样做的人。别人做一遍给他看,他就学会了。
  • Why Person: 喜欢问是为什么的人。首先得理解了为什么要那样做,然后才能去做。

How Person应该看railscasts,读Ruby on Rails Guides。还有这里http://www.pragprog.com/screencasts

Why Person应该读Ruby Programming LanguageThe Well-Grounded Rubyist, aka Ruby for Rails 2nd edition

我自觉是Why Person。但是看视频和例子,对我也很有帮助。

两本Way书:

  • The Ruby Way for Why Person
  • The Rails Way for How Person

学习者的境界

第一层,写了一些Rails程序

已经可以熟练编写Rails程序了。还要继续学习,离大师的境界还很远。

问自己一些问题:

  • 会写测试吗?
  • 会自己写Rails插件吗?
  • 会打gem包吗?
  • Javascript技术熟不熟悉?
  • CSS呢?
  • 数据库技术呢?

这时候你还有很多弱点。但你已经可以去贡献一点代码给其他开源项目了。

第二层,写了不少Rails程序,写了一些Rails插件,写了一些Gem包

这时候,你已经对RoR非常熟悉了。可以去读点Ruby的源代码了。

这个时候,该学点其他的编程语言了,如Python,Java,Scala,Clojure,Objective-C,Ocaml,Scheme之类。总之,只要你感兴趣的编程语言,这时都可以学一点。并不一定要成为这些语言的专家,但要试着去理解它们的利与弊。这会拓宽你的视眼,也能加强你的Ruby技术。

也就是说,编程语言的学习,当精通一门之后,就开始多多益善。

第三层,比任何人精通RoR,甚至可以背诵Ruby源码了

这时你应该去试着改善Ruby 1.9,以及其他的Ruby实现,如JRuby,IronRuby,MacRUby和Rubinius。你还应该贡献点代码给Rails 3了。

你已经是大师了。

Reference