From: Michael Bostler Date: 2010-11-14T08:15:10+09:00 Subject: Re: help sorting objects by their instance field My attempt at refactoring the query method would leverage array#select. I'd probably do something like: def query(name) matches = @persons.select do |person| full_name = [person.first_name, person.last_name].join(' ') full_name =~ Regexp.new("\\b#{name}\\b") end puts "Found #{matches.size} matches for that name." end Basically, if the last line of the select block returns false or nil, that element will not be returned. In this case, the last line in the block converts the "name" parameter into a regular expression and encloses it in word boundary delimiters (so that searches with, for example, "ti" will not return people named "tim"). The =~ method will return the index of the first match in the full_name string, or nil if no match is found. In other words, =~ only returns non-nil/non-false on matches, and the select method will pick those matches up -- Posted via http://www.ruby-forum.com/.