From: "D. Deryl Downey" Date: 2013-01-25T13:19:37+09:00 Subject: Re: General Ruby syntax questions Err if you've used C++ even a little then 99% of that should make perfect sense. What exactly don't you understand? Scott Price wrote: > -- Can sombody please break these down and let me know what each line of > these examples are doing. I've used c++ a little, but I'm new to Ruby. > Any response would be greatly appreciated. > > > > > > > def pow(base, exponent) > result = 1 > > i = 1 > while i<= exponent > result = result * base > i += 1 > end > > result > end > > ------------------------------------ > def sum(nums) > total = 0 > > i = 0 > while i< nums.count > total += nums[i] > > i += 1 > end > > # return total > total > end > > ------------------------------------- > def is_prime?(num) > i = 2 > while i< num > is_divisible = ((num % i) == 0) > > if is_divisible > # divisor found; stop and return false! > return false > end > > i += 1 > end > > # no divisors found > true > end > ------------------------------------- > def primes(max) > primes_arr = [] > > i = 2 > while i< max > if is_prime?(i) > # i is prime; add it to the array > primes_arr<< i > end > > i += 1 > end > > # return primes_arr > primes_arr > end > -- D. Deryl Downey "The bug which you would fright me with I seek" - William Shakespeare - The Winter's Tale, Act III, Scene II - A court of Justice.