Computer Science Canada

Really brief explanation of why Ruby's blocks are awesome

Author:  wtd [ Sat Apr 04, 2009 12:18 pm ]
Post subject:  Really brief explanation of why Ruby's blocks are awesome

Python:
for i in range(1, 10):
    print "Hello"


Ruby:
9.times do
    puts "Hello"
end

Author:  BigBear [ Sat Apr 04, 2009 12:26 pm ]
Post subject:  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.

Author:  wtd [ Sat Apr 04, 2009 12:35 pm ]
Post subject:  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).

Author:  octopi [ Sat Apr 04, 2009 12:47 pm ]
Post subject:  Re: Really brief explanation of why Ruby's blocks are awesome

This seems simpler to me:
Perl:
print "Hello" x 9;

Author:  Clayton [ Sat Apr 04, 2009 12:51 pm ]
Post subject:  RE:Really brief explanation of why Ruby\'s blocks are awesome

Ruby:
puts "Hello " * 9

Author:  BigBear [ Sat Apr 04, 2009 1:00 pm ]
Post subject:  RE:Really brief explanation of why Ruby\'s blocks are awesome

I know this has nothing to do with it but

Turing:

put repeat ("hello\n", 9)

Author:  saltpro15 [ Sat Apr 04, 2009 1:22 pm ]
Post subject:  RE:Really brief explanation of why Ruby\'s blocks are awesome

for the hell of it
c++:

#include <iostream>
using namespace std;

int main(){
for (int a = 0; a<10; a++){
cout << "Hello\n";
}
return 0;
}

Author:  btiffin [ Sat Apr 04, 2009 5:15 pm ]
Post subject:  Re: Really brief explanation of why Ruby's blocks are awesome

Falon 0.9
code:
9.times(.[printl "hello"])

but the auto-bound parameter for the times method allows
code:
3.times(printl)
0
1
2
which I wouldn't use; too Perly obscure.
code:
9.times([printl, &1])
or
9.times({ n => > n })
or
f = 9.times
f({ n => > n})

I'll stop now. To be honest, I think the Ruby syntax really does, and deserves the, win for clarity on this one.

Cheers
Edit; typos

Author:  wtd [ Sat Apr 04, 2009 6:21 pm ]
Post subject:  RE:Really brief explanation of why Ruby\'s blocks are awesome

All nice tries when it comes to string multiplication and printing the result, but what if I want to replace the string printing with some other action and do it 9 times? Smile

Author:  saltpro15 [ Sat Apr 04, 2009 7:54 pm ]
Post subject:  RE:Really brief explanation of why Ruby\'s blocks are awesome

I see your point wtf, I think it's time I started learning some Ruby

Author:  apomb [ Mon Nov 02, 2009 3:10 pm ]
Post subject:  RE:Really brief explanation of why Ruby\'s blocks are awesome

After seeing this:
Ruby:
(1..15).each do |i|
        puts"*"*i
    end


i was sold on blocks

edit:typo

Author:  DtY [ Mon Nov 02, 2009 7:11 pm ]
Post subject:  RE:Really brief explanation of why Ruby\'s blocks are awesome

A few days ago I discovered Enumerable#inject, it's become my favourite use of blocks:

Ruby:
#n!
(1..n).inject(1){|a,b| a*b}

#sum arr
arr.inject(0){|a,b| a+b}

Author:  Tony [ Mon Nov 02, 2009 7:39 pm ]
Post subject:  RE:Really brief explanation of why Ruby\'s blocks are awesome

Checking that all conditions in the array evaluate to true
Ruby:

arr.inject(true){|a,b| a && b}

Author:  Insectoid [ Mon Nov 02, 2009 8:20 pm ]
Post subject:  RE:Really brief explanation of why Ruby\'s blocks are awesome

Hmm, as my own interest in Ruby demands, I must know what inject does. Does it 'inject' each element of the array into the expression in {...}? If {...} is a block, can I also do it like:

[syntax='Ruby']
arr.inject (true) do |a, b|
a && b
}
[/syntax]

I suppose the (true) part is a conditional, which returns true if the block returns a value equal to the conditional? or is it a 'filler' element in case there is more than one parameter, and it must test against say 'n-1' where n is the first element of the array?

Author:  DtY [ Mon Nov 02, 2009 8:27 pm ]
Post subject:  Re: RE:Really brief explanation of why Ruby\'s blocks are awesome

insectoid @ Mon Nov 02, 2009 8:20 pm wrote:
Hmm, as my own interest in Ruby demands, I must know what inject does. Does it 'inject' each element of the array into the expression in {...}? If {...} is a block, can I also do it like:

[syntax='Ruby']
arr.inject (true) do |a, b|
a && b
}
[/syntax]

I suppose the (true) part is a conditional, which returns true if the block returns a value equal to the conditional? or is it a 'filler' element in case there is more than one parameter, and it must test against say 'n-1' where n is the first element of the array?

a and b were pretty bad variable names for me to choose, say I did:
(1..9).inject(1){|result, item| result * item}
It starts with 1, the first index in 1..9:
yield 1, 1 #First is the total, which starts with 1, second is the index
=> 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)

Author:  Insectoid [ Mon Nov 02, 2009 9:03 pm ]
Post subject:  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?

Author:  DtY [ Mon Nov 02, 2009 9:07 pm ]
Post subject:  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

Author:  wtd [ Tue Nov 03, 2009 11:54 am ]
Post subject:  Re: RE:Really brief explanation of why Ruby\'s blocks are awesome

insectoid @ Tue Nov 03, 2009 10:03 am wrote:
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]

Author:  apomb [ Tue Nov 03, 2009 12:16 pm ]
Post subject:  RE:Really brief explanation of why Ruby\'s blocks are awesome

Thanks DtY, that actually helped alot in understanding enums and blocks.

Author:  Insectoid [ Tue Nov 03, 2009 5:05 pm ]
Post subject:  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.


: