#!/usr/bin/env ruby # This code comes from http://ruby-it.org/pages/Filtrare+con+select+e+reject # Check the page for copyright notice and explanations def linea_lunga?(linea) linea.length > 7 end risultato=[] File.new('esempio.txt').each do |l| risultato.push(l) if linea_lunga?(l) end puts File.new('esempio.txt').select { |x| linea_lunga?(x) } def con_numeri(linea) /[0-9]/.match(linea) end puts File.new('esempio.txt').select { |x| linea_lunga?(x) and con_numeri(x) } lunga=proc do |lin| lin.length > 7 end num=proc do |lin| /[0-9]/.match(lin) end puts File.new('esempio.txt').select(&lunga).select(&num) >> [1,2,3,4,5,6,7,8,9,1000].reject {|i| i%2==1 } # i dispari => [2, 4, 6, 8, 1000]