Ask HN: PHP & Python/Ruby?

18 points by hella ↗ HN
I'm decently proficient in PHP. I chose it initially because it was easy to learn and (very easy) to deploy.

I just finished a series of small web projects, and am looking to start something more ambitious. Before I do, I want to reevaluate my choice of language: should I switch to python or ruby?

Let's say I would switch to ruby...

1) How much more productive/efficient would it make my coding?

2) How hard and expensive (relatively) would it be to deploy?

3) Why shouldn't I switch?

4) Other major ruby advantages?

And finally, how should I go about the transition if its worth making the jump to ruby/python?

31 comments

[ 2.8 ms ] story [ 82.0 ms ] thread
This is fairly even. I would take a look at each and decide which style you like better.

In general... ruby there are many ways to do things ( bit of a perl influence there ), python tends towards one sanctioned way to do things. That is a horrible overstatement but somewhat true.

I would advise you to send a few days with each and see which seems more natural. I prefer ruby w/ its blocks and more smalltalk and lisp-ish feel to things but I did python programming for a number of years and you will be fine learning either.

You should switch from php even if you go back to php to learn something new and expand your knowledge AND don't stop there, keep learning and go father afield later. Pick a functional language like scheme, clojure, haskell etc and learn that. Get up and cozy w/ smalltalk for some serious OO learning. Spend sometime with Erlang. Never stop switching what you use to learn even if you keep going back to other things to pay your bills- Java, Perl & C++ payed by bill for 7 years but during that time I learned Dylan, Smalltalk, Scheme, Python & several more.. each one I learned made me a better programmer.

Don't compare languages, compare frameworks, and don't ask us, do the tutorials.
(comment deleted)
This was down voted but it brings up valid a point. There are a lot of posts asking which language the OP should learn. It's redundant and very subjective. Yet at the same time people love their languages, their religion, what helps them wake up in the morning and so these posts are very popular.
More importantly I was making the point that people rarely write websites "in Ruby" or "in Python", they write them in "Rails" or "Django".

People often write websites "in PHP" and that is a big, heavy and sharp double edged sword: it is this flexibility that has contributed largely to PHPs terrible reputation amongst languages.

The OP is looking to "take things more seriously" so that means developing PHP with a framework, hence it's not relevant to compare Python to Ruby to PHP - the language and it's syntax will have far less impact on the OPs experience than will the structure, idioms and deployment requirements of various frameworks written in each of those langauges.

People, please read this parent again. Why are we all comparing PHP to ruby/rails? If you want to be fair use Zend or any other framework to get apples to apples.
I originally learned web development through LAMP. I boosted my proficiency when I learned to program in MVC via codeigniter and kohanaphp.

Then I learned Ruby on Rails.

Kohana and codeigniter set up an MVC environment much like Ruby on rails does. The biggest difference for me is that if you look at kohana, you see how much effort it takes to get PHP to act in a way that Ruby does natively.

Kohana sets up PHP so you have access to all sorts of nifty methods by accessing the system Kohana classes and helpers. In ruby, everything is an object.

So long story short, I find DSL's in PHP try very hard to emulate how Ruby already behaves. So I prefer Ruby frameworks like sinatra and Rails.

I wish I could upvote this more times.

I love Kohana, and I'm looking forward to learning Ruby/Rails this year. I have a few small projects lined up to work on while learning, which should be pretty fun, too.

If you DO decide to switch from PHP to Ruby on Rails make sure to get very familiar with Ruby before you dive into Rails, otherwise learning rails will be very frustrating. You can also try out Sinatra before Rails. Sinatra is a great lightweight framework that I've been using extensively. It is awesome. http://www.sinatrarb.com/

Regarding whether or not to switch I will say this: you can build whatever you want with either language. The big reason you should switch is the fun and challenge of learning something new. Both languages/frameworks have their advantages and disadvantages. You will be LESS productive if you switch languages at first because you will be new and will have to learn the 'ruby way of doing things'. Eventually you may end up more productive in the long run, but productivity is influenced by so many factors and language/framework is just one of them.

Regarding deployment, Heroku (free for small apps) is a good option for if you don't want to deal with deployment much. Linode ($20/mo minimum) is great if you need to do anything fancy and/or want to have root access to your deployment environment.

http://railstutorial.org is the best rails tutorial I've seen

Good Luck!

I found that RailsTutorial explains Ruby well enough that I didn't need to learn it separately from Rails. The author covers Ruby concepts not found in PHP (such as ranges and symbols) in chapter four. Everything else could be filled in by looking at the online documentation.
I was in your boat 3 years ago. I decided on Ruby on Rails and am very fortunate I made that decision. It's really not as much about efficiency, productivity, expense, etc. as it is enjoyment of working in a language/framework that's highly elegant. Learning Ruby on Rails (or Python/Django) will far outweigh productivity gains for your first project. However, it will be worth it in the end.

For me, it mostly comes down to the fact that there's almost always an easy answer for questions in Rails. Pagination? Sorting? AJAX? There's usually a "Best" way too, that makes it easy in deciding which library/plugin to choose.

I have to recommend ruby if only for Ruby Version Manager and bundler. Ruby also has multiline lambdas (code blocks). Python only has single line lambdas.

Disclosure: I've been using ruby (and rails) for 2 years and I am absolutely addicted to it.

I've been using ruby (and rails)... I am absolutely addicted to it.

This experience is pretty common. I think that says a lot.

Python only has single line lambdas.

Rumor control. While it is true that the lambda construct in Python is only one line, Python allows you to define functions in any scope, and pass those functions around. They are full closures. So anything you wanted to do with multi-line lambdas you can.

Yes, but I've found it to be rather cumbersome compared to ruby and under utilized. The standard library doesn't seem to make much of an attempt to pass around functions whereas in ruby you are often using code blocks to replace loops.

Ruby's ability to hijack the scope of a given block via class_eval is amazingly useful in creating DSLs. Creating DSLs in ruby is trivial. The router for Rails 3 is a perfect example of this. Python makes this far more difficult.

Either Python or Ruby will put you in good hands -- I suspect Ruby is probably better overall if web development is your main focus, but I have to admit that I prefer Python, just due to its amazing infrastructure (scipy / numpy, SAGE, NLTK, so on and so forth). Spend a couple of days working through tutorials in either language, and then just pick one or the other based on your gut; you really can't go wrong and you can always learn the other one later. Linode is what I recommend to set up Python hosting, but it means you have to get your hands a bit dirty with installing packages and the like, which you may or may not be interested in doing. You can also just download Virtualbox and set up a VM locally and experiment for free.
I can only speak for Ruby - it's what I chose when I moved from PHO development several years ago.

1. I am much more productive in Ruby than I ever was in PHP because the language is more consistent and testing is built in. But it takes a long time to understand Ruby so that you can be proficient.

2. it's not hard nor expensive anymore. Dreamhost hosts Rails applications just like PHP apps. Linode.com has VPS setups and scripts to get you going. Heroku is free for small apps. Worry about building something deployable first, and then tackle deployment.

3. You shouldn't switch if you can't take the short-term productivity hit. You're not going to be as productive in Ruby right off the bat because it's new. My interns all agree that the first two weeks of Ruby with me are pretty rough, (80 hours or so) but after that they take off. But it takes a lot longer to program Ruby like Ruby and not like PHP.

4. My opinion will get in the way here, and a lot of the really cool stuff isn't really for beginners. But I am so happy I have the tools I need to accomplish the crazy tasks I've gotten myself into.

5. RailsTutorial.org is a great start - the pdf is free. Also, I run RailsMentors.org - maybe you can find someone there willing to guide you. However, be prepared to come with questions and do some work. Also, find a local user group and hang out. A lot of Ruby people love to help you get started as long as you're willing to put forth the effort.

Feel free to ping me with questions. bphogan on twitter.

Best of luck.

I think Rails is much more stable. The Python frameworks seem to release more often (for me, at least). Ruby also has good libraries for web specific purposes, Python has more overall. For example, a website might release an API and a wrapper for Ruby, Python, and Java. However, an obscure project might have an official Python wrapper but not a Ruby wrapper.
1. That depends a lot on you, and it goes without saying that you won't likely be more productive on your first project or two while you're still learning the language and libraries. Web frameworks in general try to do some of the work for you. Rails, in particular takes a lot of the busy work out of writing a "typical" web app. I think I'm probably twice as productive on a big project with Rails than with bare PHP. I haven't used any PHP frameworks.

2. There's a bit of a tradeoff between ease of deployment and cost. Specialized hosting services like Heroku are very easy, but can get pricey, especially as you scale. Setting up and running your own VPS can be very cheap[0], but requires learning basic *nix server administration. You should probably learn that anyway, as a web developer, but it does add to the barrier to entry.

3. Switching could be a distraction from actually getting stuff done. You can expect a significant drop in productivity as you learn, especially if PHP is the only language you know. There's a lot of new stuff in Ruby, and Rails has a decent-size API. You may also want to consider the universality of PHP on cheap web hosts if you're writing code for other people to run on their own servers.

4. Ruby, as a language seems to me like the designers really kept the user experience in mind. It's similar to the advantages often cited for Apple products. I've heard similar things about Python, but I haven't used it nearly as much. Another advantage I've found over PHP is that example code in both Ruby and Python is much more likely to be exemplary. There's a lot of example code for PHP floating around on forums and blogs that would be better used for counterexamples.

[0] prgmr.com starts at $5/month, though that plan isn't enough to handle much traffic.

I've taken a different approach switching to ruby(on rails). I was eager to learn a framework to simplify stuff in php, so I learned CakePHP. This MVC experience eventually led me to the bigger brother ruby on rails and from there, I bought the pickaxe book and learned ruby. Was this the best way? definitely not :)

So my advice is: First, work a weekend with bare ruby, there are lots of good and extensive resources on the web. Second, if you enjoyed working with ruby, try another weekend learning basic MVC Rails. Good resource is rails for zombies (http://railsforzombies.org/), where the basics are well shown, and you can work through this on one weekend. Third, try converting one of your small projects to rails. There will be a lot of hiccups and frustration, but once the first project is done, you will simply love ruby.

I hope this helped you a bit, although I have not directly answered your questions (mostly because the given answers are the ones I would write, too).

Short version: I'm in the same boat. I have a lot of experience in PHP (personal + professional) projects. I invested my time in learning frameworks (Symfony, Doctrine + Propel) and have tons of plugins and utilities classes. Feel free to investigate languages and learn tricks. But, don't switch unless the return on investment is high enough.

Personally, I ended up switching for my personal projects only. I look at my PHP code and despite my skills the language is simply limiting me. It's limiting my approach to problems, the readability/flexiblity of my code and the solutions I implement. Even if I have a saying in which technology I implement my solutions with, most of the web dev stuff is fairly well done in PHP _especially_ if you're using sophisticated frameworks. That's why, for clients, I'm still using PHP (for now)

Long version:

1) Ruby and Python are excellent languages. Elegant, readable, dynamic and in general well implemented. Personally, I prefer Ruby because I find it well designed and more intuitive than Python. Ruby is full OOP where Python has those built-in functions and I never know if the call to open and close a file is built-in or part of the File class. In general, Ruby tries to put things together in classes/modules and avoid "lost" functions.

Also, you can fully customize stuff in Ruby. To me, that's an advantage because after a couple of layers in your business model, if your want to do something like Product < Product2 instead of Product.function < Product2.function. Some people abuse it but it really helps simplifying your code when you have a lot of abstraction in your model.

3) Why shouldn't you switch? Like I said, it depends on the kind of dev you're doing. Most PHP programmers are enthusiastic hobbyists and PHP is more than enough to satisfy their needs. So, if you don't see a net return on switching to Ruby, don't. It's worth investigating languages but rarely worth switching e.g Haskell taught me a lot about modularity and gave me a new perspective on programming but I'm not gonna implement my next web app in Haskell

About the transition:

In my case, I like to experiment with languages using a problem. For example, I have a couple of batches in PHP and I needed threading. Guess what my first Ruby app was? So, find a project or something you'd like to build and ideally it's something you can't do with your current language or maybe something that is easier to build with Ruby.

Anyway, I enjoyed writing this stuff down. Hope it helps.

If you really want to excel and learn, pick up Java. It is so completely different from PHP that you'll learn a tonne (and likely appreciate PHP more).

The upside is that there is always a market for Java developers (not that there isn't one for PHP but it's good to have options).

I'm sure that the script kiddies will bury this post and tell you to learn Erlang, Ruby, etc (which are all good in their own right) but that's this old cat's 2 cents.

You don't mention why one should not choose Erlang, Ruby, etc, nor why one should in fact choose Java. That you also lump Erlang and Ruby together, two very different languages, with different advantages, and of completely different paradigms; further undermines the credibility of your suggestion.

Your reference to script kiddies? The icing on the cake my friend.

Whoa. I hit a nerve there.

He didn't ask what he shouldn't learn. He asked what he should learn.

I suggested that he learn Java because it is so completely different from PHP. That's all.

Depends on your circumstances in part.

Will you be doing scientific programming? Python.

Graphical games? Python.

Web apps? Ruby or Python are about comparable.

Do you really really really want multi-line lambdas? Ruby.

Do you really want to make DSL's? Ruby.

Do you really want code that's easy to read and maintain years from now after it's no longer fresh in your mind? Python

Do you want a language with a smaller number of rules for your brain/tools to parse (ie. a more "regular" syntax)? Python.

Do you want as much "magic" and implicit behavior as possible in the language? Ruby

Do you prefer things to be explicit, but still concise? Python

Do you want to be seen as cool and hip as possible? Definitely Ruby rather than Python

Will you likely be writing lots of CPU-bound code and therefore need a language that has a fast runtime characteristics? Python

Do you think Perl is a good thing? Ruby. A bad thing? Python.

Do you want a lot of open source projects and a rich ecosystem? Ruby and Python are about comparable

Do you want to hob-nob with folks who are, on average, generally younger? Ruby

Do you think "end end end end end end" everywhere looks dumb as hell? do Python

Do you think having to type "self self self self self" a lot in classes feel wrong to you? do Ruby.

Want the one of the two that's most popular/blessed/supported by Google? Python

Agree on most of the points. Just two I disagree on.

> Do you really want code that's easy to read and maintain years from now after it's no longer fresh in your mind?

In my experience ruby code isn't inherently less readable than python if you know ruby moderately well. Ruby has more syntax but it's standard library is fairly clear and it's easy to look up.

> Do you think Perl is a bad thing?

I think it's a horrible language and absolutely I love ruby. :) Granted, I learned ruby before I learned perl.

This post in itself is begging to be turned into a 5 minute webapp :)
I love CodeIgniter for PHP but every time I peek into RoR I feel like home. The main turn off for me so far was the deployment overhead. With CodeIgniter you can just carry your entire web app in a thumb drive and plug it into any machine that has the basics (webserver, database server etc.) setup. With RoR I felt the deployment is more of an overhead. Dealing with SSH scripts and such haven't felt comfortable to me yet and I have gone back to CodeIgniter for its simplicity and so far there wasn't much of a disappointment. Pretty much everything can be done in CodeIgniter it seems, one way or the other.

Having said that I completely agree that RoR would be the way to go given the elegance of Ruby and the Rails framework. Will surely give it a try couple more times until I feel comfortable dealing with all the aspects of RoR. Until then CodeIgniter for me...

I'm a Ruby coder and I like it, but I think that you should make a reasonable attempt to study both Ruby and Python at the same time.

The key skill that you're looking to pick up is actually what professionals think of as "full stack web development". That is, you should aim to understand lots of things:

- MVC web frameworks like Rails and micro-frameworks like Sinatra

- MySQL and non-relational datastores like MongoDB

- web and proxy servers like thin and nginx

- Redis! it's like a Swiss Army knife... but also Memcached

- jQuery and Haml/Sass

- Backbone and websockets

I could go on and on... the point is that each of these things are awesome for different tasks. You should try your hand at all of them!

Bonus points: I think that SproutCore is going to be a very BIG deal in 2011. You could get the jump on everyone.

I am in a similar situation.. I consider myself very proficient in PHP (and have been doing it full-time for 2 years since school). Being forced to use one language all the time didn't sit too well with me. Recently, I picked up a few python books and made a new app just for fun with python/GAE. 100% free (minus the book costs) and it was eye opening in many regards. Seeing all the beautiful language constructs that python has available compared to PHP made me very jealous. I fully intend to jump into the Ruby world eventually to see what all the fuss is about - However, I recommend you do what I did and try some small stuff with GAE just because of the ridiculously easy setup/deployment. Lets you focus more on the language and coding.