From: 7stud -- Date: 2007-10-05T12:25:25+09:00 Subject: Re: Add files to array matching pattern Leon Bogaert wrote: > Tried this: > > dir = "/home/leon/"; test = Array.new; test = > Dir.new(dir).entries.collect { |x| x == 'filezilla' ? x : next } > > But that didn't work also. It gives back: > > [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, > nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, > nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, > nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, > nil, nil, nil, nil, nil, nil, nil, nil, nil] The reason you get that output is: 1) collect adds the value returned by the block to a results array. 2) There is no file named "filezilla" in the directory /home/leon 3) That means the value of the block is always whatever is produced by the statement: next. If you try this: [1, 2, 3].each {|x| puts next} you'll get the error r9test.rb:1: void value expression I think that means that the next statement is one of the few statements in ruby that doesn't produce a value. So what is the value of the block when the next statement gets executed as the last statement? If you try this: results = [1, 2, 3].collect {|x| next} p results the output is: [nil, nil, nil] Look familiar? Apparently, a block returns nil by default if the statements in the block do not produce any values. -- Posted via http://www.ruby-forum.com/.