From: ara.t.howard@... Date: 2006-11-01T09:16:06+09:00 Subject: Re: still more relentless non-repetition On Wed, 1 Nov 2006, Giles Bowkett wrote: > ok, I have this Rails code which I want to make more Rubyish. > > controller: > > def tracks > @term = params[:term] || '' > if @term.blank? > @tracks = [] > else > @tracks = Track.find_by_contents @term > end > end do you really want to search twice here if tracks is called twice? i'm guessing you'd want def tracks return @tracks unless defined? @tracks t = params[:term] @tracks = if t.nil? or t.blank? [] else Track.find_by_contents t end end > def tracks > @term = params[:term] || '' > case params[:thing_to_search_for] > when "tracks" > if @term.blank? > @tracks = [] > else > @tracks = Track.find_by_contents @term > end > when "beats" > if @term.blank? > @beats = [] > else > @beats = Beat.find_by_contents @term > end > when "profiles" > if @term.blank? > @profiles = [] > else > @profiles = Profile.find_by_contents @term > end > when "users" > if @term.blank? > @users = [] > else > @users = User.find_by_contents @term > end > end > end def tracks return @tracks unless defined? @tracks t = params[:term] @tracks = if t.nil? or t.blank? [] else model = self.class.const_get t.capitalize model.find_by_contents t end end __never__ use eval on user data in webaps. regards. -a -- my religion is very simple. my religion is kindness. -- the dalai lama