Ruby vs PHP for the web

J

Joel VanderWerf

Robert Klemme wrote in post #960960:

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.

What makes this so slippery a question is what "treat as" means.

Does

a = [1,2,3]
s = "count = #{a.length}"

mean that a Fixnum is treated as a String? It may look so at first
glance. But what's really happening is that #to_s is called on the
Fixnum. So String interpolation is using, as you say, duck typing, to
convert types. I don't really know whether this counts as "treat as" or not.

IMO, the most clear-cut example of weak typing is this:

$ cat a.c
#include <stdio.h>
main(){
float x = 1.23;
printf("%d\n", (int)x);
printf("%d\n", *(int*)&x);
}

$ gcc a.c
$ ./a.out
1
1067282596

Not even perl is weakly typed, in this sense.
 
R

Robert Klemme

Robert Klemme wrote in post #960960:

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.

So you are claiming that Ruby is weakly typed. It is not. Ruby does
not allow an instance of A to be treated as an instance of B. Duck
typing really only means that Ruby does not have interfaces and uses
dynamic typing. In other words, all instances answering a particular
set of methods can be used in a piece of code, e.g.

def append(a,b)
a << b
end

append $stderr, "Last warning!"
append ARGV, "--help"
append [1,2,3], 4

See also Joel's example from C. This is not possible in Ruby.

Kind regards

robert
 
D

Damjan Rems

Ruby Me wrote in post #959480:
Thank you guys,

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

I had 15 years of programing after me when I ended my programing carrier
and got a job as system administrator. My skills were mostly with
languages with little code to write. I Started with RPG on IBM
mainframes and ended with Clipper on DOS and windows.

But since programming is like a hobby I tried to learn something new and
in early 2000 PHP looked like a good language(Java to me is like a
Cobol. Too much lines about nothing). But I never got really into it. As
example I had a program which has created dates to schedule Backup Exec
jobs for a whole year and write result to file. About 60 lines in
Clipper. It took me about 3 days when I start learning PHP and I ended
up with 150 lines. But I never did anything sirius in PHP.

Few years later I heard about Ruby. It looked really cool. I had the
same program written after 4 hours of learning and in 35 lines. And
after that I didn't done any program again in any other language. 3
months later I installed rails and done my first web application after
another month (I knew something about HTML before).

I've been using ruby for 5 years now. And I don't use anything else. I
script Linux, Windows, web apps with rails. It is so simple. I have
programmed Intranet application in my company which connects to Oracle,
MS-SQL, Postgres (my favorite). And thats from the guy who never thought
that would ever make a program for Internet.

by
TheR
 
M

Mike Stephens

Robert Klemme wrote in post #961079:
Duck typing really only means that Ruby does not have interfaces and >uses
dynamic typing.

OK so what's the difference between dynamic typing and weak typing?

My take on strong typing is you cannot assign an object of Type A to a
variable of Type B unless you use a transform method so you convince the
'compiler' you intended to do this (and such a method exists). It's more
to do with trapping programmer errors than to do with making compilers'
lives easy. Where that leaves you on adding (etc) an object of Type A to
an object of Type B is less clear but I would be inclined to think that
falls into the same camp.
 
R

Robert Klemme

Robert Klemme wrote in post #961079:
dynamic typing.

OK so what's the difference between dynamic typing and weak typing?

Dynamic typing only says something about _when_ types are evaluated.
In dynamically typed languages variables typically do not have a type
and typing is enforced at runtime.

Weak typing OTOH is about _what_ you can do with "something". In C
you can easily cast a char* to a int* and use it as that (e.g. for
math). That's weak typing. Such things are impossible in a strongly
typed language - either dynamic or static typed.
My take on strong typing is you cannot assign an object of Type A to a
variable of Type B unless you use a transform method so you convince the
'compiler' you intended to do this (and such a method exists).

You describe a language with strong typing *and* static typing. In a
language which does not have static typing the statement you make is
meaningless because variables do not have a type there (as in Ruby).
It's more
to do with trapping programmer errors than to do with making compilers'
lives easy.

That is a description of a goal but not of a technology.
Where that leaves you on adding (etc) an object of Type A to
an object of Type B is less clear but I would be inclined to think that
falls into the same camp.

The mere fact that you can add a string and an integer does not tell
you much about the nature of the type system. In Ruby you have
#coerce and in C++ you have operator overloading. Both languages are
statically typed. If you define an operator like in the C++ program
below you still have static and strong typing (at least for this piece
of code, you can still cast via void* in C++) even though you can add
strings and integers.

Kind regards

robert


#include <string>

int operator + (const std::string& s, const int i) {
return i + s.length();
}


int main(int argc, char* argv[]) {
std::string x(argv[0]);
return x + argc;
}
 
R

Robert Klemme

The mere fact that you can add a string and an integer does not tell
you much about the nature of the type system. =A0In Ruby you have
#coerce and in C++ you have operator overloading. =A0Both languages are
statically typed.

Typo!!! This should of course have read "strongly typed".

Sorry

robert

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

James Vince

PHP has more lines of code in the actual File than ruby. If you take a
controller (in Rails) and compare it to a PHP program, PHP has more
lines of code. However Ruby has libraries to do most of this code for
you, if you include the libraries that you use Ruby has much more lines
of code.

But because people who program Object Oriented Code tend to consider
library code to not part of the actual program (as it is part of the
API) they see it as "the actual code you write is less".

Having large libraries of code (coupled with the fact that Ruby has a
slow interpreter) does make Ruby somewhat slower performance wise,
however a Ruby programer would argue that because Ruby language is more
efficient that PHP by re-using code and by using symbols where necessary
instead of strings that makes up for it's slow rendering time.
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

Cool story, bro
 
R

Robert Klemme

PHP has more lines of code in the actual File than ruby. If you take a
controller (in Rails) and compare it to a PHP program, PHP has more
lines of code. However Ruby has libraries to do most of this code for
you, if you include the libraries that you use Ruby has much more lines
of code.

That sounds convincing on first sight yet I am missing hard facts.
Can you put figures on this for a concrete application? We could
start with versions of Ruby and PHP you are comparing here.
But because people who program Object Oriented Code tend to consider
library code to not part of the actual program (as it is part of the
API) they see it as "the actual code you write is less".

Which is true of course. You only need to write and test your own
code and can rely on the library author's QA for their code. Of
course, from time to time you'll find a bug in library code as well.
Having large libraries of code (coupled with the fact that Ruby has a
slow interpreter) does make Ruby somewhat slower performance wise,
however a Ruby programer would argue that because Ruby language is more
efficient that PHP by re-using code and by using symbols where necessary
instead of strings that makes up for it's slow rendering time.

Again, this sounds convincing on first sight but it actually isn't.
Using a library does not automatically mean that more code is executed
or an application is slower. Without hard facts all these statements
stand on a very shaky ground and I would certainly not base my choice
of language or framework on what you wrote.

Kind regards

robert
 
A

Ammar Ali

PHP has more lines of code in the actual File than ruby. If you take a
controller (in Rails) and compare it to a PHP program, PHP has more
lines of code. However Ruby has libraries to do most of this code for
you, if you include the libraries that you use Ruby has much more lines
of code.

This is not entirely true. PHP does not have a "standard library",
instead all functionality is either built into the interpreter or
comes in an extension. If you take PEAR into account then the higher
number of LOCs that is characteristic of PHP will change the picture.
As for frameworks, Zend looks pretty bloated to me for what it does.
Rails might be bigger, LOC wise, but it does a lot more too.
But because people who program Object Oriented Code tend to consider
library code to not part of the actual program (as it is part of the
API) they see it as "the actual code you write is less".

I believe Ruby's libraries tend to have less code than PHP's
libraries. Even though I don't have numbers to back this up, I have a
hunch that mixins in Ruby, which PHP doesn't have, play a role in
reducing the LOC count in libraries.
Having large libraries of code (coupled with the fact that Ruby has a
slow interpreter) does make Ruby somewhat slower performance wise,
however a Ruby programer would argue that because Ruby language is more
efficient that PHP by re-using code and by using symbols where necessary
instead of strings that makes up for it's slow rendering time.

Large libraries of code do not necessarily mean lower performance
overall. Ruby's parser is very fast, and once the is code loaded, and
cached by the OS/FS, the effect of it's size on performance is
negligible. Unless the code is being reloaded on every rendering
request, of course, which should not be done if one is concerned with
performance.

Regards,
Ammar
 
J

John Morrice

Having large libraries of code (coupled with the fact that Ruby has a
slow interpreter) does make Ruby somewhat slower performance wise,
however a Ruby programer would argue that because Ruby language is
more efficient that PHP by re-using code and by using symbols where
necessary instead of strings that makes up for it's slow rendering
time.

Neither PHP or Ruby are high-performance languages. [1] To bring up the
obvious example, neither are (usually) compiled.

So it seems a little strange to compare the two in this manner. Kinda
like racing snails! (Cute little snails...)

In the functional programming community, a language design is deemed to
be elegant when it has a small 'core', because then you get
benefits like not needing lots of syntax, a more organized compiler -
indeed, this minimalism is something the functional community strive
for! [2]

I'm don't know how Ruby is implemented, but it certainly *feels* like a
small language, at least to me. Initializing an object - a method
call. Defining a class can be done with method calls. All you need
are methods and objects, and constants:

A = Class.new

A.module_eval do
def neatsville
puts "That's Jazz"
end
end

a = A.new
a.neatsville

=> That's Jazz

It's easy to see how the more traditional way of defining a class can
be decomposed into these simpler operations - this is definitely
subjective, but from a minimalist point of view that is a *good thing*.

How would you do this with PHP? Is it even possible? Someone else will
have to take this up, for I am a PHP noob.

[1] Ruby and PHP are both close to the bottom of the (flawed) benchmarks
game, way outpaced by rockets like Javascript and Smalltalk...
http://shootout.alioth.debian.org/u32/which-programming-languages-are-fastest.php

[2] Simon Peyton Jones - Implementing functional languages: a
tutorial. The chapter on the G-Machine is quite accessible.
http://research.microsoft.com/en-us/um/people/simonpj/papers/pj-lester-book/

Johnny
 
L

lkfken

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.

I wrote programs in Assembly.
I wrote programs in BASIC (i know...but it was considered IN during
the 70s/early 80s), then PASCAL, JAVA, and now Ruby.

If you have develop programs in Assembly before, then you'd know you
have write so many lines in order for it to do so little...

Thus comparing number of lines is really pointless, because if you
want, you could add a lot more lines yourself by not using any
"standard libraries"

This is one of the reasons why OOP is the mainstream now -- re-
usability.
But the trade off of this re-usability is "performance". This is due
to all the overheads you added by using the "standard libraries" But
this could be neglected with better hardware performance...

So to answer your question, you have to tell us WHY you want to create
a web application with less code? If you answer is "Performance,"
then you picked a wrong measurement (# of lines). Instead you should
use "number of cycles"

Kenneth Leung
 
R

Robert Klemme

I wrote programs in Assembly.
I wrote programs in BASIC (i know...but it was considered IN during
the 70s/early 80s), then PASCAL, JAVA, and now Ruby.

If you have develop programs in Assembly before, then you'd know you
have write so many lines in order for it to do so little...

Thus comparing number of lines is really pointless, because if you
want, you could add a lot more lines yourself by not using any
"standard libraries"

Interesting tidbit: research has shown that developers have roughly
the same productivity in lines of code per day _independently of
programming language_. From that automatically follows that with more
preproduced code at your hands (either in libraries, interpreters or
compilers) your overall performance increases.
This is one of the reasons why OOP is the mainstream now -- re-
usability.
But the trade off of this re-usability is "performance". =A0This is due
to all the overheads you added by using the "standard libraries" =A0But
this could be neglected with better hardware performance...

I am not sure this analysis is correct: typically, if you have a
library with a good implementation efforts have gone into it to
provide a good interface and to optimize it. While it might be
relatively easy to implement the same functionality yourself, doing so
in a robust, bug free *and* efficient way typically involves
significantly more efforts. Of course, if one uses a library in
unintended ways it might perform poorly - but then you are using the
wrong tool anyway.
So to answer your question, you have to tell us WHY you want to create
a web application with less code? =A0If you answer is "Performance,"
then you picked a wrong measurement (# of lines). =A0Instead you should
use "number of cycles"

Absolutely! And: /first/ measure, /then/ judge.

Kind regards

robert

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

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Interesting tidbit: research has shown that developers have roughly
the same productivity in lines of code per day _independently of
programming language_. From that automatically follows that with more
preproduced code at your hands (either in libraries, interpreters or
compilers) your overall performance increases.
Though you take a hit learning how to use the preproduced code. I have spent
a lot of time failing to learn libraries and conventions.
 
W

w_a_x_man

2010/11/12 Jesús Gabriel y Galán <[email protected]>:





And even worse:

$ perl -le 'print 2+"2"'
4

In awk, "+" always means arithmetical addition; string concatenation
is indicated by juxtaposition.

C:\>mawk "BEGIN{ print 2 + '2'; print 2 '2' }"
4
22
 
W

w_a_x_man

In awk, "+" always means arithmetical addition; string concatenation
is indicated by juxtaposition.

C:\>mawk "BEGIN{ print 2 + '2'; print 2 '2' }"
4
22

Or even

C:\>mawk "BEGIN{ print '2' + '2'; print 2 2 }"
4
22
 
L

lkfken

Interesting tidbit: research has shown that developers have roughly
the same productivity in lines of code per day _independently of
programming language_.  From that automatically follows that with more
preproduced code at your hands (either in libraries, interpreters or
compilers) your overall performance increases.

Thank you.
My English is not very good. So from what I understand, you are
saying that productivity has not direct relationship to the
programming language?

Based from your comment, I did a little research.

http://www.codinghorror.com/blog/2005/08/are-all-programming-languages-the-same.html

Will you please point me to the source of your comment?

Anyhow, I was not talking about productivity. I was trying to say
measuring the web framework in "number of lines" in 2 different
languages is almost pointless...esp. when it is used as a reference on
performance.

In Ruby, I know you can increase the script's performance by writing
an extension in C. But then, is it necessary? One can obtain this
increase of performance by running the program in different hardware
(ie: 386 vs 486 vs core 2 dual )
I am not sure this analysis is correct: typically, if you have a
library with a good implementation efforts have gone into it to
provide a good interface and to optimize it.

You are right. I love libraries/OOP... I cannot imagine if I have to
write EVERY routines myself in order for my script to work.

But what if it is an mission-critical scenario? First, I prob.
shouldn't use Ruby to begin with. Second, if I have to use Ruby, in
order to gain back few extra cycles, I might need to write my own
extension in C instead of using "standard libraries".

I am already way off the original topic. Sorry.
 
R

Robert Klemme

Thank you.
My English is not very good. =A0So from what I understand, you are
saying that productivity has not direct relationship to the
programming language?

IIRC the statement was that independent from programming language the
number produced lines of code per day that were error free is roughly
identical. So this was not only about the writing itself but also
about ensuring quality.
Based from your comment, I did a little research.

http://www.codinghorror.com/blog/2005/08/are-all-programming-languages-th= e-same.html

Will you please point me to the source of your comment?

Unfortunately I don't have a URL handy. Candidate sources are
http://www.amazon.com/dp/3540520392
http://www.amazon.com/dp/0201835959
http://www.amazon.com/dp/0932633439

But the source may be a totally different one. It's been a while that
I found that bit.
Anyhow, I was not talking about productivity. =A0I was trying to say
measuring the web framework in "number of lines" in 2 different
languages is almost pointless...esp. when it is used as a reference on
performance.

And I absolutely agreed (and agree). I was digression because I found
that bit interesting and thought others would, too.
You are right. =A0I love libraries/OOP... =A0I cannot imagine if I have t= o
write EVERY routines myself in order for my script to work.

Exactly. And that does not only save you writing effort but testing
effort (and probably documentation effort) as well.
But what if it is an mission-critical scenario? =A0First, I prob.
shouldn't use Ruby to begin with.

And you would base that decision on what?
Second, if I have to use Ruby, in
order to gain back few extra cycles, I might need to write my own
extension in C instead of using "standard libraries".

I am already way off the original topic. =A0Sorry.

Drifting off topic can reveal interesting insights and produce
discussions which lead to new knowledge. :)

Kind regards

robert


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

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,780
Messages
2,569,608
Members
45,246
Latest member
softprodigy

Latest Threads

Top