From: Daniel Carrera Date: 2003-06-12T12:28:06+09:00 Subject: Re: Ruby advantages over Perl -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thu, Jun 12, 2003 at 11:19:11AM +0900, Jason Creighton wrote: > If I were you, I would not stress individual advantages, because > somebody will say "Language X can do this too!" Ruby is a great language > because it's easy to use. It's easy to write clean OO. There's a > thousand little conveniences spread throughout the language. It doesn't > make you write stuff just so the compiler's happy. Yes, I agree. I think it's best to give some code examples. This is what I'd suggest. - Present a simple problem. - Show a very natural and simple solution in Ruby. - Ask the reader, "how would you do this in Perl?". The issue is not that Perl "can't do it". Rather, it should be an example where the Perl solution is either difficult, or not as clean as Ruby's. Here are two examples. The point of them is not that it's hard to "do" in Perl, but rather, that the Ruby solution has a very natural syntax. Example 1: ========== Task: Compute factorials, combinations, permutations and binomial - ----- distributions. Code: - ----- # Factorial. class Fixnum def _! self < 0 and raise "Factorial only defined for non-negative numbers" self == 0 and return 1 self * (self - 1)._! end end # Permutations. def P(n,x) n._!/(n-x)._! end # Combinations. def C(n,x) n._!/( (n-x)._! * x._! ) end # Binomail def Binomial_P(n,p,x) C(n,x) * p**x * (1-p)**(n-x) end Note: - ----- The last three functions are nearly *identical* to what you'd see in the textbook. There is nearly *zero* mapping between math notation and Ruby's syntax. THAT is the power of Ruby's OO. Ruby's OO allow me to make Ruby fit whatever my mental model is. Not the other way around. Example 2: ========== Task: Vector algebra in R^n (addition, subtraction, scalar - ----- multiplication). Code: - ----- class Array def coerce(other) return self, other end def *(n) n.kind_of?(Numeric) or raise "must multiply by a scalar" self.map{|e| e*n} end def +(arr) arr.class == Array or raise "must add/subtract vectors" arr.length == self.length or raise "lengths don't match" sum = [] arr.length.times {|i| sum[i] = self[i] + arr[i]} sum end def -(arr) self + (arr * (-1) ) end end Usage: - ------ >> [1, 2, 3] + [2, 3, 4] #=> [3, 5, 7] >> [1, 2, 3] * 2 #=> [2, 4, 6] >> 2 * [1, 2, 3] #=> [2, 4, 6] Again, Ruby's OO lets you adapt the language so you can write things in correspondence to your mental model. The Perl code would be something like: &sum(\@vector1, \@vector2) &prod(\@vector, $scalar) And the implementation itself would be more complicated. I hope that some of this helps. - -- Daniel Carrera | OpenPGP fingerprint: Graduate TA, Math Dept | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7 7A88 UMD (301) 405-5137 | http://www.math.umd.edu/~dcarrera/pgp.html -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (SunOS) iD8DBQE+5/M/nxE8DWHf+OcRAsLyAJ9BnT0VwDx1/EQIgx41w12oi3dT4gCfXYvA M8acDWe+YKAlFs1llqAgf8M= =bwB/ -----END PGP SIGNATURE-----