From: Ben Bleything Date: 2006-09-13T07:24:12+09:00 Subject: Re: getting the last word in each line On Wed, Sep 13, 2006, Joe Van Dyk wrote: > Is there a prettier way to read a file and return the last word in each > line? Well, there's #readlines... File.readlines('somefile').map {|f| f.split.last} ...which is a little nicer. > What if I wanted to search the lines (based on a regex) and return the > last word in each line that satisfied the search criteria? File.readlines('somefile').map do |f| next unless f =~ /regex/ f.split.last end But you'll have nils everywhere that a line was next'ed over... File.readlines('somefile').reject{|f| f !~ /regex/}.map {|f| f.split.last} Someone else might have an elegant solution, but these will work :) Ben