Ruby vs PHP for the web

Hi

I am not here to say that PHP is better or the opposite, I am here to
ask, how web development in Ruby compared to PHP. Does Ruby use the same
tools? for example MySQL and Apache?

And the most interesting point, which language can create a web
application with less code? (I am talking about the languges not the
frameworks).

Thanks.

2010/11/4 Ruby Me [email protected]:

And the most interesting point, which language can create a web
application with less code? (I am talking about the languges not the
frameworks).

But Ruby with Rails does create a web app with much less code than
others.

On 10-11-03 09:49 PM, Ruby Me wrote:

Hi

I am not here to say that PHP is better or the opposite, I am here to
ask, how web development in Ruby compared to PHP. Does Ruby use the same
tools? for example MySQL and Apache?

yes it works with the same DBs and others, not sure what you mean by
same tools?

And the most interesting point, which language can create a web
application with less code? (I am talking about the languges not the
frameworks).

Thanks.

Why not do something in PHP and then try doing the same thing in Ruby &
Rail, what speaks to you will be the better choice for you!

Here is a rails 3 screencast to give you an idea about rails 3

http://rubyonrails.org/screencasts/rails3


Kind Regards,
Rajinder Y. | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1

Mike S. wrote in post #959251:

If you use eRuby, then the two are very similar in the way you code, the
way you inter-operate with middleware, and in the amout of code needed.

That’s true, but I’d say that most ruby app designers don’t write their
app logic within eruby.

To try a different way of doing things, have a look at Sinatra. A
starter Sinatra app is 4 lines:

require “sinatra”
get “/” do
“Hello world!”
end

Run your app standalone from the commandline, or from within Apache or
Nginx using Phusion Passenger.

Your next step would be to use templates for response pages - templates
can either be stored inline, or in separate files - and helper methods
for code snippets to be called within templates. Then connect it to a
database using an ORM layer of your choice.

eruby is one of many choices for template languages (I prefer HAML), but
what you’re trying to do is keep your request processing logic out of
the template, and centralised in your application code.

If you use eRuby, then the two are very similar in the way you code, the
way you inter-operate with middleware, and in the amout of code needed.

On Nov 3, 2010, at 6:49 PM, Ruby Me wrote:

I am not here to say that PHP is better or the opposite, I am here to
ask, how web development in Ruby compared to PHP. Does Ruby use the same
tools? for example MySQL and Apache?

I went from PHP to Rails and have never looked back. I recommend using
Rails 3. It easily connects to MySQL and PostgreSQL. Running
development code is quick as it comes with a built-in web server and
database.

I don’t know if you’re using the Zend framework, but Rails is a MVC
framework. Zend’s framework is relatively new and came out after I made
my switch to Rails. I also haven’t compared Rails to Python’s Django.
I have used WebObjects though, and I find Rails to be extremely elegant
with respect to that system.

And the most interesting point, which language can create a web
application with less code? (I am talking about the languges not the
frameworks).

Ruby, the language that facilitates Rails and all the other favorite
frameworks, is unequivocally less code. It’s a big reason why I won’t
go back to PHP.

Jose

Jose Hales-Garcia
UCLA Department of Statistics
[email protected]

On 10-11-04 10:11 PM, Ruby Me wrote:

Thank you guys,

It would be helpful if anyone writes any conde in PHP and then writing
it in Ruby? I want to see the difference and how the code will be less.

you must be sitting in agony trying to decide between php and ruby =P,
code is only half the battle, with rails it comes with code generators
and rake tasks which make life really easy for a web developer! you’re
not going to see this if some cut-n-paste static code!

please don’t ask a ruby or rails developer to write php code samples for
you, it’s just inhumane! =P


Kind Regards,
Rajinder Y. | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1

On Fri, Nov 5, 2010 at 7:11 AM, Rajinder Y. [email protected]
wrote:

please don’t ask a ruby or rails developer to write php code samples for
you, it’s just inhumane! =P

I agree. With all those dollar signs, arrows (->), and mandatory
semicolons, it’s like licking sandpaper. And that’s just the syntax.

Cheers,
Ammar

Thank you guys,

It would be helpful if anyone writes any conde in PHP and then writing
it in Ruby? I want to see the difference and how the code will be less.

On Fri, Nov 05, 2010 at 11:11:18AM +0900, Ruby Me wrote:

Thank you guys,

It would be helpful if anyone writes any conde in PHP and then writing
it in Ruby? I want to see the difference and how the code will be less.

As someone who has written a fair bit of both PHP and Ruby for the Web
over the years – and very little of the Ruby actually using something
like Rails – I think I know something about the subject when I say
this:

  1. Code snippets will not give you a good idea of how much one or the
    other might save you any total code weight in the final version. Pick
    one small task that can be represented in a small snippet, and one
    language wins; pick another, and the other small task wins. In fact,
    for
    the smallest tasks, PHP will almost certainly win because of its
    literally thousands of core functions – which, by the way, is not a
    sign of good language design. Last I checked, there were about 3100
    core
    functions, which is obscenely complex for a procedural language, and
    pollutes the main namespace of the language to an insane degree.

  2. Final code weight to accomplish a specific task is nowhere near the
    same thing as the amount of code you have to write, anyway. A lot of
    code that gets written for any given non-trivial project is ultimately
    deleted, usually to be replaced by other code. One of the benefits of
    Ruby is that it tends to lend itself to finding the “right” idiom for a
    given task sooner rather than later. PHP is not as good at that, and
    tends to require a heck of a lot of refactoring as it scales up past the
    size of a relatively trivial application. A lot of projects simply do
    not do that much refactoring, but the end result is a largely
    unmaintainable mess of an application, like WordPress. Reading the
    source of WordPress is a bit like using thumbtacks for contact lenses.

  3. Ruby’s object model lends itself to far better code organization than
    anything PHP offers. This means that, even in cases where Ruby might
    have the same amount of source code (or even more) to accomplish the
    same
    task, it’s a lot easier to understand because of the way it can be
    divided up into discrete chunks that make sense. PHP’s unreasonable
    facsimile of an object model and bolted-on half-baked namespace support,
    by contrast, tends to creak under its own weight when developing any
    nontrivial application.

All told, PHP is easier to use for the most trivial, single-page dynamic
templates, and harder to use to accomplish for anything more meaningful.
If all you’re going to do is maintain a simplistic Website that contains
nothing but a little bit of simple logic in otherwise static pages,
perhaps populating some content areas from a couple of extra files, and
learn as little as possible, you might want to go with PHP. If you’re
going to do some real programming, though, I recommend Ruby; it’ll
facilitate development more on larger projects, it’ll encourage better
coding practices, and it’ll be more fun.

A lot of people complain about the superficial ugliness of Perl. PHP,
however, inherits pretty much all of Perl’s flaws as a language, and
almost none of its benefits. Ruby inherits a substantial percentage of
Perl’s benefits, and not so many of its flaws – even by the markedly
uncharitable opinion of what constitutes a “flaw” that may Pythonistas
would give you.

I have recently stumbled across a brilliant characterization of PHP as
training wheels without the bike. It’s hilarious – and having used
Ruby
for a while, it feels pretty true, too. I suppose your mileage may
vary.

Note that a lot of this has been a liberal mix of opinion and fact. I
encourage you to think about what I said and do a little investigating
to confirm what I’ve said for yourself, rather than just taking my word
for it. If you want my opinion, though, it’s easily summed up:

I’m glad I don’t have to write PHP these days.

On Mon, Nov 8, 2010 at 2:56 PM, Andola S. [email protected] wrote:

For creating a web application, you need to use ruby on rails and not
simply ruby. of course you can create application with much less code
and you don’t have to write sql queries as in PHP; database interface
can be handled with object relational mapping in RoR.

Correction: You can use Rails or any other Ruby-based webframework,
but you don’t have to, nor do you need no.


Phillip G.

Though the folk I have met,
(Ah, how soon!) they forget
When I’ve moved on to some other place,
There may be one or two,
When I’ve played and passed through,
Who’ll remember my song or my face.

For creating a web application, you need to use ruby on rails and not
simply ruby. of course you can create application with much less code
and you don’t have to write sql queries as in PHP; database interface
can be handled with object relational mapping in RoR.

On Mon, Nov 08, 2010 at 11:18:24PM +0900, Phillip G. wrote:

On Mon, Nov 8, 2010 at 2:56 PM, Andola S. [email protected] wrote:

For creating a web application, you need to use ruby on rails and not
simply ruby. of course you can create application with much less code
and you don’t have to write sql queries as in PHP; database interface
can be handled with object relational mapping in RoR.

Correction: You can use Rails or any other Ruby-based webframework,
but you don’t have to, nor do you need no.

In fact, you do not need any framework at all. Ruby can be run
comfortably via CGI, mod_ruby, or eruby, for instance (and I, at least,
wouldn’t call eruby a “framework”).

Charles C. wrote in post #960599:

Ruby uses
strong, though dynamic, typing.

Can you explain what you mean? That seems a contradiction in terms.

On Wed, 3 Nov 2010 20:49:22 -0500, Ruby Me [email protected]
wrote in [email protected]:

I am not here to say that PHP is better or the opposite, I am here to
ask, how web development in Ruby compared to PHP. Does Ruby use the same
tools? for example MySQL and Apache?

As others have said, yes. There are Ruby libraries to allow you to
use most, possibly even all, of the external resources that you’re
used to using from PHP. Examples include MySQL, PostreSQL,
ImageMagick, encryption libraries, etc.

And the most interesting point, which language can create a web
application with less code? (I am talking about the languges not the
frameworks).

That’s not really a useful question, as it’s fairly uncommon to create
web applications without a framework of some sort these days.

Having worked with both, I’ll tell you why I like Ruby a lot more than
PHP.

  1. PHP is a poorly designed language that suffers from a number of
    problems, including design by committee and the misguided effort to
    “make programming easy for nonprogrammers”. Google “PHP sucks” and
    you’ll find plenty of detailed articles enumerating the issues with
    PHP. Some of these have been corrected (or at least deprecated) in
    version 5 and more will be corrected in version 6, but others are
    still there, notably the horrible inconsistencies in the standard
    library.

  2. PHP’s support (in version 5) for OO programming is rather anemic.
    You get single inheritance only; there is no support for multiple
    inheritance or mixins. While you can use __get() and __set() to
    implement properties
    http://en.wikipedia.org/wiki/Property_(programming), this is
    nasty and can lead to huge switch statements on classes of any
    complexity. The standard library is still heavily procedural. It
    doesn’t support namespaces.

Ruby has mixins, which makes multiple inheritance less necessary and
properties (called attributes). Namespaces can be faked using
modules. Finally, Ruby was designed to be an OO language from the
beginning, while PHP has tacked OO features onto a procedural
language.

  1. PHP uses weak typing, which leads to all sorts of problems with
    ensuring that code behaves the way that you intended. Ruby uses
    strong, though dynamic, typing.

  2. There are a ton of very cool tools surrounding Ruby that can make
    your life a lot easier. PHP has many fewer of these.

  3. Ruby attracts smart, experienced programmers. PHP attracts people
    who are new to programming. Don’t underestimate this one. If you’re
    going to interact with a community and learn from people, which of
    these would you pick?

Ruby Me wrote:

And the most interesting point, which language can create a web
application with less code? (I am talking about the languges not the
frameworks).

Ruby cause of the high dynamic approach and a cleaner language design.

Ruby allows you to build DSLs (Rails is such a thing) to make things
shorter. You have to understand that the Ruby language itselfs can be
modified (not the whole language but parts).

A important thing is DRY, don’t repeat yourself. Ruby allows you to
write programs where you don’t have to double your code many times.

In many languages you can see, how people have to copy’n’paste code and
modifiy only small things. It’s not the way how to do this in ruby.

Yes, you can repeat yourself in Ruby, but you don’t have to as much as
in other languages.

I read the online documentation about ruby and thought: Great, this
people made concepts first and tried to repeat this concepts in every
piece of the language. So you have an very consistent language with less
pit falls.

One thing is: Everything is an object. Yes, it means, everything is an
object and you can use the pattern of an object everywhere.

Another thing is: Everything is dynamic. So you can change an object and
a class at runtime (a class is an object too but at a higher level), can
define new methods. Yes, the internals of the language are objects and
you can work with them like any other object.

This is simply GREAT. With this dynamic approach you can build your own
DSL.

And the third thing is very important: Every piece of code can be
handled as an object. So you can write methods, which expects code as a
argument. This is a very heavy used concept in Ruby and is called
“blocks” where the given code is the so called block.

You write for example an algorithm for traversing a data structure. And
you have code which can handle the or some elements of the structure. In
Ruby you can easily split this code into two parts. You should do this
because the two problems, knowledge about the data structure and
knowledge about the elements are complete independent from each other.

You could exchange the elements with other types of elements and you had
to traverse the data structure the same way.

Or you could use the elements somewhere else in another data structure.

So you should split the code and ruby supports you very well on that
task.

So you can write shorter, cleaner and less error prone code. And yes,
writing ruby code is much more fun than writing PHP. You can feel the
support for abstraction. On the other side writing PHP code feels like
walking through a shelter. You feel thick walls everywhere. The language
controls you very hard.

Regards
Oli

On Thu, Nov 11, 2010 at 11:50 PM, Mike S. [email protected]
wrote:

Charles C. wrote in post #960599:

Ruby uses
strong, though dynamic, typing.

Can you explain what you mean? That seems a contradiction in terms.

It means that the language imposes restrictions about how different
types are mixed, and avoids implicit conversions of types, among other
things. Compare:

Javascript (weak typing):

2 + ‘2’ => ‘22’

Ruby (strong typing):

2 + ‘2’ => TypeError: String can’t be coerced into Fixnum

On the other hand:

Most of the type checking (if there actually is some type checking) is
done at runtime, and while values have types variables do not:

Java (static typing):

int i = 3;
i = “abc” => Error

Ruby (dynamic typing):

i = 3
i = “abc”

It’s also related to the very powerful duck typing, which you can search
around.

Jesus.

On Fri, Nov 12, 2010 at 1:54 PM, Josh C. [email protected]
wrote:

Strong and weak typing - Wikipedia

written, by defining String#coerce:

class String
def coerce(num)
return num.to_s , self
end
end

2 + ‘2’ # => “22”

I know that, but that’s not an implicit conversion performed by the
compiler, it’s explicitly implemented in the classes.

Citing from the above wikipedia article:

The object-oriented programming languages Smalltalk, Ruby, Python, and
Self are all “strongly typed” in the sense that typing errors are
prevented at runtime and they do little implicit type conversion,

Also, in Ruby, you cannot change the class of an object.

Jesus.

2010/11/12 Jess Gabriel y Galn [email protected]

It means that the language imposes restrictions about how different

Is Ruby strongly typed?

2 + 2.0 # => 4.0

Here it has implicitly converted the integer 2 to a float, in order to
be
able to add them. You can even make your specific example pass, exactly
as
written, by defining String#coerce:

class String
def coerce(num)
return num.to_s , self
end
end

2 + ‘2’ # => “22”

Robert K. wrote in post #960960:

There seems to be some disagreement what exactly “strongly” and
“weakly” means here.

To me it’s pretty straightforward. An object of Class A cannot be
treated as an object of Class B - the language system detects and
prevents this. Ruby does not follow this approach. It uses duck typing
which is the opposite.