
-----------------------------------
wtd
Sat Apr 04, 2009 12:18 pm

Really brief explanation of why Ruby's blocks are awesome
-----------------------------------
for i in range(1, 10):
    print "Hello"

9.times do 
    puts "Hello"
end

-----------------------------------
BigBear
Sat Apr 04, 2009 12:26 pm

RE:Really brief explanation of why Ruby\'s blocks are awesome
-----------------------------------
so python only uses 2 line lol

I tried to see what to learn after turing and looked at ruby and read about how amazing it was and tried it. At the same time I read about python and decided ruby seemed better.

Ruby shortens and simplifys everything it makes it hard to follow it. 

Python sort of follow turing's sequences.

Ruby seems it would be better once you understand it more.

-----------------------------------
wtd
Sat Apr 04, 2009 12:35 pm

RE:Really brief explanation of why Ruby\'s blocks are awesome
-----------------------------------
The number of lines is not what's important in the examples shown.  What is important is how clearly each expresses what the program is trying to do (say "hello, world" 9 times).

-----------------------------------
octopi
Sat Apr 04, 2009 12:47 pm

Re: Really brief explanation of why Ruby's blocks are awesome
-----------------------------------
This seems simpler to me:
print "Hello" x 9;

-----------------------------------
Clayton
Sat Apr 04, 2009 12:51 pm

RE:Really brief explanation of why Ruby\'s blocks are awesome
-----------------------------------
puts "Hello " * 9

-----------------------------------
BigBear
Sat Apr 04, 2009 1:00 pm

RE:Really brief explanation of why Ruby\'s blocks are awesome
-----------------------------------
I know this has nothing to do with it but


put repeat ("hello\n", 9)

-----------------------------------
saltpro15
Sat Apr 04, 2009 1:22 pm

RE:Really brief explanation of why Ruby\'s blocks are awesome
-----------------------------------
for the hell of it

#include 
using namespace std;

int main(){
for (int a = 0; a 1 * 1 = 1
yield 1, 2 #One is still the total, two is the index
=> 1 * 2 = 2
yield 2, 3 #Two is the total, three is the index
=> 2 * 3 = 6
yield 6, 3 #Six is the total, three is the index
=> 6 * 3 = 18
And so on

[edit] To answer your question more directly, true is the initial value, which is put through the filter for each index, each time the block is yielded, the first value passed is what is returned from the last (Except for the first element, where it is the argument to the function)

-----------------------------------
Insectoid
Mon Nov 02, 2009 9:03 pm

RE:Really brief explanation of why Ruby\'s blocks are awesome
-----------------------------------
I see, so 'true' is 'a' the first time the block is run. 'a&&b' would be 'a' the next time. So this iterates recursively then?

-----------------------------------
DtY
Mon Nov 02, 2009 9:07 pm

RE:Really brief explanation of why Ruby\'s blocks are awesome
-----------------------------------
Kinda recursively, say you do:

[A,B,C].inject(true){|a,b| a && b}
It would evaluate to
( (true && A) && B) && C ) )
This might help: http://ruby-doc.org/core/classes/Enumerable.html#M003140

-----------------------------------
wtd
Tue Nov 03, 2009 11:54 am

Re: RE:Really brief explanation of why Ruby\'s blocks are awesome
-----------------------------------
I see, so 'true' is 'a' the first time the block is run. 'a&&b' would be 'a' the next time. So this iterates recursively then?

I doubt this is implemented recursively, but it is a gateway drug to functional programming, where it would be done recursively.

[code]let rec inject f init v =
    match v with
        [] -> init
      | x::xs -> inject f (f init x) xs
in
    inject (fun a b -> a + b) 0 [3; 4; 5][/code]

-----------------------------------
apomb
Tue Nov 03, 2009 12:16 pm

RE:Really brief explanation of why Ruby\'s blocks are awesome
-----------------------------------
Thanks DtY, that actually helped alot in understanding enums and blocks.

-----------------------------------
Insectoid
Tue Nov 03, 2009 5:05 pm

RE:Really brief explanation of why Ruby\'s blocks are awesome
-----------------------------------
Yeah, I realized that while walking to school today. While recursion would take the problem and work backwards, to find the smallest problem which it CAN solve, this is already at the beginning.
