Ruby vs PHP for the web

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

Strong and weak typing - Wikipedia

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

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.

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_Class.html

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

2 + ‘2’ # => “22”

There seems to be some disagreement what exactly “strongly” and
“weakly” means here. This becomes apparent of you read the
complementary article on Strong and weak typing - Wikipedia in
Wikipedia.

Kind regards

robert

On 11/12/2010 08:35 AM, Mike S. wrote:

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.

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.

On 11/12/2010 05:35 PM, Mike S. wrote:

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.

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

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

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 :slight_smile:
http://rubyconf2008.confreaks.com/evil-code.html 4m55s

On Sat, Nov 13, 2010 at 01:35:41AM +0900, Mike S. wrote:

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.

Not quite. The type of an object of class A is never changed. Rather,
a
method of class A is executed to produce a return value of class B. The
class of the object of class A never changes.

:~> irb
irb(main):001:0> a = 3
=> 3
irb(main):002:0> a.class
=> Fixnum
irb(main):003:0> b = "foo"
=> "foo"
irb(main):004:0> c = "#{b} #{a}"
=> "foo 3"
irb(main):005:0> a.class
=> Fixnum

Would you prefer that a programming language have no way to do anything
with an integer in conjunction with a string if it’s called “strong
typing”?

Robert K. 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.

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

On Mon, Nov 15, 2010 at 8:57 AM, Mike S. [email protected]
wrote:

Robert K. 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?

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

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;
}

On Mon, Nov 15, 2010 at 9:30 AM, Robert K.
[email protected] wrote:

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.

Typo!!! This should of course have read “strongly typed”.

Sorry

robert

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.

Cool story, bro

On Mon, Nov 15, 2010 at 10:45 AM, James V. [email protected] wrote:

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

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

On Mon, Nov 15, 2010 at 11:45 AM, James V. [email protected] wrote:

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

On Nov 3, 5:49pm, Ruby Me [email protected] wrote:

Thanks.


Posted viahttp://www.ruby-forum.com/.

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

On Tue, Nov 16, 2010 at 8:30 PM, lkfken [email protected] wrote:

On Nov 3, 5:49pm, Ruby Me [email protected] 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?

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

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”. This is due
to all the overheads you added by using the “standard libraries” But
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? If you answer is “Performance,”
then you picked a wrong measurement (# of lines). Instead you should
use “number of cycles”

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

Kind regards

robert

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

Javascript (weak typing):

2 + ‘2’ => ‘22’

And even worse:

$ perl -le ‘print 2+“2”’
4

On Nov 17, 2:09am, zuerrong [email protected] wrote:

$ 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

On Nov 17, 4:27am, w_a_x_man [email protected] wrote:

$ 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

Or even

C:>mawk “BEGIN{ print ‘2’ + ‘2’; print 2 2 }”
4
22

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.