Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Really brief explanation of why Ruby's blocks are awesome
Index -> Programming, Ruby -> Ruby Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
BigBear




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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)
saltpro15




PostPosted: 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;
}
btiffin




PostPosted: 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
Sponsor
sponsor
wtd




PostPosted: 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
saltpro15




PostPosted: 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




PostPosted: 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
DtY




PostPosted: 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




PostPosted: 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}
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Insectoid




PostPosted: 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




PostPosted: 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)
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: