From: Chad Perrin Date: 2007-03-09T03:38:04+09:00 Subject: Re: A couple of questions from a Ruby neophyte On Thu, Mar 08, 2007 at 09:27:01PM +0900, Alex Young wrote: > > >- A language with a flexible array or array-like object. One of the > >things I love about PHP is the ease with which an array can be > >populated (eg "MyArray[] = 'a new value';" adds "a new value" as a new > >item to the array MyArray). > Ruby's arrays aren't quite as feature-packed as PHP's arrays, which are > more like mutant hashes with strange super-powers. A Ruby array is > created like this: The only quibble I have with your post is the above characterization of PHP's array variables. It's more accurate, I think, to say that by failing to more carefully separate normal arrays and associative arrays (hashes) from each other, PHP ends up merely pretending to have two array types while in fact it only has one -- and it's pretty broken in some annoying ways. The problems with PHP arrays are mostly in secondary effects, such that the way they're implemented imposes limitations on the language as a whole, even though looking at the arrays in a vacuum they don't seem terribly broken at first glance. PHP handling of arrays isn't really something I've put too much thought into analyzing, so I'm afraid I can't come up with a succinct description of my objections to how they work just yet, but suffice to say that despite the atrocious syntactic gyrations needed to deal with complex data structures in Perl thanks to the list-flattening problem I still prefer working with complex data structures in Perl over working with them in PHP. I think your uses of the terms "mutant" and "strange" are appropriate to describing PHP arrays, but "super-powers" is a bit less so, and "feature-packed" is only useful to describe PHP arrays where "feature" is contrasted with "useful functionality". Because of its design -- and the way arrays work is a decent demonstration of this fact -- PHP's usefulness as you get beyond simple templating development drops off significantly. The more complex the language gets, the more acrobatic your work-arounds must become. > > arr << 'c' This is a beautiful example of where Ruby handles arrays better than PHP handles them. Doing it the PHP way: $arr = 'c' . . . is confusing and limiting. It hinders code readability and maintainability, and restricts the potential uses of the assignment operator outside of appending values to an array by essentially overloading the operator haphazardly. > > > > >- A language with strong Regular Expression abilities, and string > >manipulation tools / methods. > Yup. Better than PHP's, in my opinion. Far, far better than PHP's. In the quartet of commonly cited web scripting languages (Perl, PHP, Python, and Ruby), PHP's regular expression handling is by far the worst. It's kludgy, inconsistent, and unwieldy. I try to avoid it. I don't like Python's much either, but it's a darn sight better than PHP's. I think I've been spoiled by working primarily with Perl regexen. PHP's regex handling is actually better than that of probably 90% of languages that have regexen at all. I just think you need to be in about the 98th percentile before you get to the languages that actually have *good* regex support, and Ruby is definitely in that category. > > >So, would anyone be able to help me work out if Ruby is the language I > >need? > I guess the thing to do would be to find a simple task you need to do, > and actually try it out, and see how it feels - and check back here if > you need help. That's how I got hooked :-) That's a pretty good take on it. The languages I'd suggest, off the top of my head, for the tasks mentioned here include: Perl Python Ruby Each has its benefits and detriments, of course. I personally am not a fan of Python, but it's still a great language in (most of) the ways that matter. Ruby's my favorite, by a long shot, for OOP. Perl may provide the most comfortable syntax for people who like a unixlike environment (including me). All three of them handle the needs outlined in the original post of this thread admirably. -- CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] "It's just incredible that a trillion-synapse computer could actually spend Saturday afternoon watching a football game." - Marvin Minsky