Ruby vs PHP for the web

R

Ruby Me

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.
 
W

wroxdb

2010/11/4 Ruby Me said:
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.
 
R

Rajinder Yadav

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 Yadav | 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
 
M

Mike Stephens

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.
 
B

Brian Candler

Mike Stephens 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.
 
J

Jose Hales-Garcia

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
(e-mail address removed)=
 
R

Ruby Me

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.
 
R

Rajinder Yadav

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 Yadav | 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
 
A

Ammar Ali

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
 
A

Andola Soft

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.
 
P

Phillip Gawlowski

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 Gawlowski

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.
 
O

Oliver Schad

Ruby said:
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
 
C

Charles Calvert

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.

3. 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.

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

5. 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?
 
J

Jesús Gabriel y Galán

Charles Calvert wrote in post #960599:

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

http://en.wikipedia.org/wiki/Strong_typing

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' =3D> '22'

Ruby (strong typing):

2 + '2' =3D> TypeError: String can't be coerced into Fixnum

On the other hand:

http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing

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 =3D 3;
i =3D "abc" =3D> Error

Ruby (dynamic typing):

i =3D 3
i =3D "abc"

It's also related to the very powerful duck typing, which you can search ar=
ound.

Jesus.
 
J

Josh Cheek

2010/11/12 Jes=FAs Gabriel y Gal=E1n said:
http://en.wikipedia.org/wiki/Strong_typing

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' =3D> '22'

Ruby (strong typing):

2 + '2' =3D> TypeError: String can't be coerced into Fixnum


Is Ruby strongly typed?

2 + 2.0 # =3D> 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' # =3D> "22"
 
J

Jesús Gabriel y Galán

Is Ruby strongly typed?

2 + 2.0 # =3D> 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 a= s
written, by defining String#coerce:

class String
=A0def coerce(num)
=A0 =A0return num.to_s , self
=A0end
end

2 + '2' # =3D> "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.
 
R

Robert Klemme

Is Ruby strongly typed?

2 + 2.0 # =3D> 4.0

Here it has implicitly converted the integer 2 to a float, in order to be
able to add them.

Strongly typed because there is no _implicit_ conversion.
You can even make your specific example pass, exactly as
written, by defining String#coerce:

Exactly: this is an *explicit* conversion of types. Implicit
conversion would be if Ruby would convert 2 to 2.0 *before* invoking a
"float + operator". But in Ruby just method :+ is invoked on instance
2 which internally uses the coerce mechanism to (hopefully) get two
instance which can properly work out how to +.

See http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Cl=
ass.html
class String
=A0def coerce(num)
=A0 =A0return num.to_s , self
=A0end
end

2 + '2' # =3D> "22"

There seems to be some disagreement what exactly "strongly" and
"weakly" means here. This becomes apparent of you read the
complementary article on http://en.wikipedia.org/wiki/Weak_typing in
Wikipedia.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
M

Mike Stephens

Robert Klemme 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.
 
J

Josh Cheek

2010/11/12 Jes=FAs Gabriel y Gal=E1n said:
I know that, but that's not an implicit conversion performed by the
compiler, it's explicitly implemented in the classes.
Gotcha, so if that same thing happened when the code was interpreted rather
than when the addition method invoked coerce, then it would be weak typing.

Also, in Ruby, you cannot change the class of an object.
I've seen some black magick ^_^
http://rubyconf2008.confreaks.com/evil-code.html 4m55s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top