Chunk

Hi,

I am a beginner in ruby and It try to undertand the concept of
“enum.chunk” in particular these 2 exemples (RDoc Documentation
classes/Enumerable.html#M003131)

" open(“/usr/share/dict/words”, “r:iso-8859-1”) {|f|
f.chunk {|line| line.ord }.each {|ch, lines| p [ch.chr,
lines.length] } "

and
" sep = “-”*72 + “\n”
IO.popen(“svn log README”) {|f|
f.chunk {|line|
line != sep || nil
}.each {|_, lines|
pp lines
}
}
"

What do those mean ? I can’t understand the way it works…

Best regards,

Jr

On Sep 12, 10:18am, Jean-Ren Saint-Etienne [email protected]
wrote:

Hi,

I am a beginner in ruby and It try to undertand the concept of
“enum.chunk” in particular these 2 exemples (RDoc Documentation
classes/Enumerable.html#M003131)

it groups consecutive elements according to the value of the block you
supply, and returns an array of arrays, one for each group

" open(“/usr/share/dict/words”, “r:iso-8859-1”) {|f|
f.chunk {|line| line.ord }.each {|ch, lines| p [ch.chr,
lines.length] } "

this opens a dictionary file, and then groups by the first character
of each line, before outputting the size of each group (i.e. the
number of words in the dictionary file beginning with that letter)

Fred

result = [2, 4, 1, 3, 5, 6, 8].chunk do |num|
num.even?
end

result.each do |even_result, a_chunk|
p [even_result, a_chunk]
end

–output:–
[true, [2, 4]]
[false, [1, 3, 5]]
[true, [6, 8]]

====

e = DATA.chunk do |line|
ascii_code_for_first_letter = line.ord
end

e.each do |ascii_code, lines|
character = ascii_code.chr
number_of_lines_in_array = lines.length
p [character, number_of_lines_in_array]
end

END
a
ant
apple
ball
basket
cake
aztec

–output:–
[“a”, 3]
[“b”, 2]
[“c”, 1]
[“a”, 1]

Note that a dictionary file would not have that last line–all the ‘a’
words would be grouped together. I’m not sure why the example does the
ord/chr conversions. You could rewrite the example like this:

e = DATA.chunk do |line|
first_character = line[0]
end

e.each do |first_character, lines|
number_of_lines_in_array = lines.length
p [first_character, number_of_lines_in_array]
end

As for your last example, the chunk() docs say this:

====
The following key values has special meaning:

nil … specifies that the elements are dropped.

If you look at the first example in my post, the elements that fail the
test are chunked into a separate group. However, the test in the log
file example is written like this:

line != sep || nil

which is equivalent to:

(line != sep) || nil

If the line in the file is a bunch of dashes, the test on the left
fails because line is equal to sep. Next, in an OR statement:

something || something

if the left side is true, then ruby doesn’t even execute the right
side–because an OR is true if either side of the OR is true. When
the left side is true, no matter
what the right side evaluates to, the OR will be true, so ruby skips
the right side. In that case, ruby also returns the value of the left
side.

But if the line in the file equals a bunch of dashes, then the left side
test fails, and so the right side is evaluated and returned.

Terrible examples, I would say.