Hi
Steve said:
Coming from other programming languages, notably Basic based, a little Java & C++, I find that passing blocks into methods is a little cryptic.
The code blocks in other languages are usually called "callback functions" and usually are just regular functions. AFAIK only Clipper has similar code blocks, delimited with [tt]{ || }[/tt]. So coming from other languages rarely helps with the syntax, but the concept has nothing new.
Let us take an example : sort some letters first alphabetically, then alphabetically but with vowels first.
Ruby:
vowel='aeiou'
data=['q','w','e','r','t','y','u','i','o','p']
alpha=data.[url=http://ruby-doc.org/core/classes/Array.html
alter=data.[url=http://ruby-doc.org/core/classes/Array.html
Perl:
$vowel='aeiou';
@data=('q','w','e','r','t','y','u','i','o','p');
@alpha=[url=http://perldoc.perl.org/functions/sort.html]sort[/url] @data;
@alter=[url=http://perldoc.perl.org/functions/sort.html]sort[/url] { (index($vowel,$a)!=-1 && index($vowel,$b)==-1)?-1:(index($vowel,$b)!=-1 && index($vowel,$a)==-1)?1:$a cmp $b } @data;
JavaScript:
vowel='aeiou'
data=['q','w','e','r','t','y','u','i','o','p']
alpha=data.[url=http://devguru.com/technologies/javascript/10559.asp]sort[/url]()
alter=data.[url=http://devguru.com/technologies/javascript/10559.asp]sort[/url]( function(a,b) { return (vowel.indexOf(a)!=-1 && vowel.indexOf(b)==-1)?-1:(vowel.indexOf(b)!=-1 && vowel.indexOf(a)==-1)?1:a<b?-1:a>b?1:0 } )
In all three cases first we let the [tt]sort[/tt] method/function do its default job, then we gave it a code block/block/callback function to get a special ordering.
Now what makes the Ruby way more cryptic then the others ?
Steve said:
What I mean is that when reading a method description (definition) there is no reference made to the fact that a block could be passed in as an argument.
The code block is just a parameter. You either know what kind of parameter can you pass to a method/function, or not. I would say, this is the same for all languages and for all parameter types.
Steve said:
If my assertion is correct then the actual method call(s) in source code could be 100's of lines away from the method definition.
Correct. Also the place(s) where a String/Integer/whatever parameter is used, can be anywhere in the code.
Note that is not mandatory to make
your methods to use code blocks. And while in core classes most of the code blocks are used for iterating, many times you can replace them with old-school looping statements :
Code:
data.each do |one|
print one,' is ',vowel.index(one)?'':'not ',"vowel\n"
end
for i in 0...data.length do
print data[i],' is ',vowel.index(data[i])?'':'not ',"vowel\n"
end
So for the beginning, if they hinder your productivity, you can quite ignore the existence of the code blocks.
If you have question related to a specific situation, show some code.
Hopefully I did not misunderstood your question.
Feherke.