From: Dave Thomas Date: 2000-12-02T13:49:27+09:00 Subject: [ruby-talk:6735] Re: perl and ruby "Joseph McDonald" writes: > A couple of things I noticed which sort of annoyed me: > I had to predeclare the hash and the scaler. Sort of. Perl's syntax allows it to take intelligent guesses about wht you intend, and then automagically make it happen. When you write $a[0] = 1; it 'knows' that 'a' is an array, and pops one in to existence if it wasn't there. That's very convenient, but that convenience comes at a price. The price is that the syntax and the semantics are inexorably linked: $a[0] is an array reference. Ruby took a different tack. It wanted to be extensible from the start. A variable is simple a reference to an object: the compiler has no idea what kind of object it is. So a[0] in Ruby could be an array reference, or a hash reference, or a parameter to a directory listing class, or... In fact, in the index of our book, we list 16 classes that support the [] method. All of which means that if you write a[0] = 1 Ruby hasn't the faintest idea at compile time what you mean, so it can't pop an array (or anything else) in to existence. The same underlying issue explains why Ruby doesn't have an autoincrement. The problem is that a++ means 'increment the value in a', but in Ruby, 'a++' is interpreted to mean 'send the operator ++ to the object referenced by a'. If 'a' references a number object (say 42), we then have a problem. '42' could increment itself, but then what. If it returned a new object, 43, then that would be fine, except 'a' would still reference the original 42. If it decided to increment itself, then all the 42's in the entire system would change. Probably not a good side effect. Now, Ruby could cheat, and convert a++ to 'a += 1' at compile time. However, I personally think that this could be misleading, tempting people to forget about the underlying object structure. 'a = a +1' is indeed a strange thing. Ruby's interpreting this as 'send the argument +1 to the method 'a'. It's one of those damned if you do, damned if you don't kind of parsing problems that come about from not requiring parentheses on method calls. > I'm not here to bash ruby, I am just starting to explore it, it looks > like a fun language, I just wanted to point out some things that > a perl person may find annoying in the hopes that perhaps some > of these things can be changed. No, I think it's great that you're here. Please keep exploring. > Here are the scripts: > > > print `date`; > x = 0; # must initialize... why? > h = {}; # again, must initialize... why? > for i in 0..10 > arr = "hello world hello word".split(/\s/) > for b in arr > h[b] = b > h[b] = h[1] > x += 1 # x++ doesn't work. bummer of the summer. > # x = x +1; # syntax error... > end > end > > print "x: #{x}\n" > print `date`; Just to put a different slant on things, you could also write the loops as x = 0 h = {} 11.times do "hello world hello word".split.each |b| h[b] = b # etc,,, end end You can write Perl in Ruby, you can write Smalltalk in Ruby, and you can write Ruby in Ruby. That's one of the joys of the language. Regards Dave