DISQUS

Developing with Style: 10 Reasons why Ruby is better than PHP - Reason #2

  • Chris Hartjes · 7 months ago
    Joel, you'll get no argument from me on this feature. The ability to open up existing classes and overload them is a great feature in Ruby (and other languages too). I think a lot of what goes on in Rails could not happen without Ruby's ability to do this sort of thing.

    The "everything is an object" paradigm that Ruby (and Python as far as I can tell) supports does lead to some very elegant code.
  • Kevin Lloyd · 7 months ago
    I'm not even sure what to make of this. IHMO, it seems a more fitting title for your post would be: "10 reasons why [I] like Ruby better than PHP."

    To argue the term "better" with "evidence" like yours is sort of misguided.

    The lack of a [return] statement hardly makes a language better. Don't get me wrong, I think that Ruby is better than PHP, but your reasons leave a lot to be desired and may be misleading.

    eg. I hate the lack of braces in Ruby; does that mean I think PHP is "better"? Based on your arguments (so far) C == PHP because you're focusing on syntax.

    Hey, I'm just saying...
  • Kevin Lloyd · 7 months ago
    HOWEVER, ... (Whoops...accidentally hit Post and saw that @Chris stole the words out of my mouth.) ... object are sexy in general.

    At work, we're making the transition from C++ (and sprinkling of VB, ugh) to C# and it's just been heavenly so far.
  • joelmoss · 7 months ago
    I suppose I do concentrate on syntax a little too much, but that's because - to me - it's important. I find that good syntax makes coding more fun, and can really increase the expressiveness of your code.

    If I don't have to type "return" to return a value, then surely that is a good thing.
  • poLK · 7 months ago
    Yes, it is sweet feature, but I tend to think that "10 Reasons why Ruby is better for Codaset than PHP" would sound (and could be vindicated) better.

    Also, if every keystroke is so important for you, you forgot to mention that rb file extension is also much much shorter then php (just kidding, and looking forward for your reason #3).
  • Mardix · 7 months ago
    Hey, I'm not a Ruby fan... but from what I see, I don't care how you will judge me, I think PHP is better.

    1st: RETURN in PHP. If I don't want to return anything, I can leave it with no return. However the last statement on ruby will be returned. What if it wasn't supposed to return anything. C'mon. You have program, not guessing. Step by step.

    2nd: Cannot redeclare a class in PHP. That is right. If you want to do extra stuff with a class, you can extend it. Class name1 extends name2{}. I don't care if you think it's easier the ruby way, but you have to make everything to have a purpose. Override a loaded class can cause confusion while you are coding. You may have wanted something else, and bam ended up with whatever you did on the loaded class. And if you really need to change something, just create another class, is that hard to do?

    3rd: :ALPHANUM: characters make sense in PHP because other characters have other stuff to do ? :, etc. But doing 3.odd?, well seriously is it hard in php-> isOdd(3) ? For readability purposes, php seems more understandable. But hey, this one is more of how you like it.

    In conclusion, the author just doesn't like PHP, even though he stated that most of his work is in PHP. One answer, whatever! (lol, jk).
    But on some serious note, you failed to give a valid point on reason #2 why ruby is better than PHP. like @kevin lloyd said: "your post would be: “10 reasons why [I] like Ruby better than PHP.”"

    But so far PHP is looking good based on your arguments, only rookies and frameworkers can fall for your "reasoning". No offense, I'm just a die hard PHP programmer, you won't put my language down like that. You want to compare, try Ruby and Python. Since they kinda follow the same syntax.... yeahhhh

    PHP 4 Life!
  • joelmoss · 7 months ago
    @Mardix: Reopening a class in Ruby is not the same as extending a class in PHP. They do completely different things and achieve completely different results.

    In PHP, if I want to redefine a method from ClassA, I either have to modift that method directly, or create a whole new class with a different name, perhaps called ClassB. Then "ClassB extends ClassA". And now I also have to change all my scripts so that they reference ClassB so as to include the redefined method.

    In ruby, I literally redefine the class and method. That's it!
  • Fredrik · 7 months ago
    @Joel: Honestly, as a lot of other people have said, you should rename this series something like "10 reasons why I prefer Rubys syntax over PHPs"

    the "return" vs "no return" is just a matter of taste, I prefer the return as it gives me an explicit return point in methods that is easy to locate when you read through the code.

    about reopening classes, it's just a way to achieve something which can be done a hundred different ways. Unless you really know what you're doing, reopening classes will mostly cause you a lot of headaches - and considering that ruby is a dynamic language (well all parts except the built in objects / c modules) you could just edit the original source and get the exact same effect, or just extend the class and allow the user of whatever you're writing to switch out which class is used. Yes it can be nice to have, at times... but it's hardly a killer feature that makes ruby stomp php.

    About allowing ! and ? in method names, well then I guess Haskell or Lips must be even better languages since they allow pretty much (almost) any character in function names. Please.
  • javier · 7 months ago
    About the return statement: people seem to be missing the point. The point is: everything in Ruby returns something. So, you can assign a variable to the result of an 'if' statement or an 'or' comparison, for example, which is quite useful.
  • joelmoss · 7 months ago
    @javier: Exactly!

    I think the problem is that coders are generally very protective about their language of choice, which makes them very closed minded when it comes to other languages.

    You really won't know for sure if something is better for you, until you really try it. Just try coding without having to using the 'return' keyword, or without having to extend classes all the time, when you can reopen them instead.
  • Fredrik · 7 months ago
    @javier and @Joel: Oh trust me I have used ruby, a lot. And I still prefer an explicit return since it's a lot easier to see.

    And I never agreed with being allowed to re-open classes either, since it leads to very hard-to-track-down bugs if you use it unwisely.

    And Joel, your last post basically says "everyone that doesn't like ruby is closed minded and stupid". I have tried Ruby, a lot (I even used it before the whole Rails-era started) - and I still don't like how a lot of stuff is implicit instead of explicit in it.

    And about this quote "I think the problem is that coders are generally very protective about their language of choice" - I don't know of a single professional (note: professional) programmer that has a "language of choice". You use the language best suited for your problem, be it Ruby, C, Java, Lisp or something else.
  • joelmoss · 7 months ago
    @Fredrick: I thinks we'll have to agree to disagree.
  • Randy · 7 months ago
    I've used a lot of languages in my day, most of which I work with ruby myself now. That being said, I feel that if you want to write more "efficient" (read less) code, use ruby...if you care about defining everything, go write C...

    I enjoy using most languages, and to be honest enjoy C++ over all of them. However, due to quick time production, I use ruby quite often because of its "efficiency".
  • Mardix · 7 months ago
    Well, ok.... What is the 3rd reason? We've been waiting all week end long including monday (which was a US holiday), and still there is no reason 3.

    C'mon... not to be disrespectful... but do you really have 10 reasons, or you just going rogue against PHP and now can't find any good reasons?

    Well, I'm still waiting for the reason #3.

    BTW, I checked your blog twice a day to make sure reason #3 is there.... still waiting...

    Peace out!
  • joelmoss · 7 months ago
    @Mardix: wow, I'll take that as a compliment. Reason #3, 4, 5, 6, 7, 8, 9 and 10 are all planned and on the way.
  • Roger · 7 months ago
    Thanks Joel, this is a good series. Re-opening classes I can see would be a very useful option, particularly as the base classes (string, integer) are available for "extending" or redefining.
  • Jason · 4 months ago
    Very interesting post Joel. However, the re-opening of classes and "use it carefully" is very telling. To me, the theory of OO is that you use inheritance to override features or add new features. In a dynamic language like PHP (or Ruby or Javascript, etc), you could even attach new methods during runtime to a particular object, rendering this "re-opening" or class definition essentially redundant and even dangerous.

    If you following the whole design pattern movement, the proper way to create objects is to use a factor method anyway, so I'm not sure if I see the point of this particular feature.
  • joelmoss · 4 months ago
    You will see the point - and the benefit - once you actually start using it. At the end of the day, I cannot persuade everyone. But if you start using Ruby, you will see!