chunk

Hi,

I am a beginner in ruby and It try to undertand the concept of
"enum.chunk" in particular these 2 exemples (http://ruby-doc.org/core/
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

Hi,

I am a beginner in ruby and It try to undertand the concept of
"enum.chunk" in particular these 2 exemples (http://ruby-doc.org/core/
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]]