Posted: Sat Apr 04, 2009 12:18 pm Post subject: Really brief explanation of why Ruby's blocks are awesome
Python:
for i inrange(1, 10):
print"Hello"
Ruby:
9.timesdo puts"Hello" end
Sponsor Sponsor
BigBear
Posted: 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.
wtd
Posted: 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).
octopi
Posted: 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;
Clayton
Posted: Sat Apr 04, 2009 12:51 pm Post subject: RE:Really brief explanation of why Ruby\'s blocks are awesome
Ruby:
puts"Hello " * 9
BigBear
Posted: Sat Apr 04, 2009 1:00 pm Post subject: RE:Really brief explanation of why Ruby\'s blocks are awesome
Posted: 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> usingnamespace std;
int main(){ for(int a = 0; a<10; a++){ cout << "Hello\n";
} return0;
}
btiffin
Posted: 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
Sponsor Sponsor
wtd
Posted: 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?
saltpro15
Posted: 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
apomb
Posted: 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).eachdo |i|
puts"*"*i
end
i was sold on blocks
edit:typo
DtY
Posted: 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}
Tony
Posted: 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
Posted: 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?
DtY
Posted: 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)