From: Daniel Schierbeck Date: 2006-08-11T03:05:14+09:00 Subject: Re: good writing in Ruby... Josselin wrote: > I know I can write it the 'ugly common way' (loop) > but I'd like to see how it can be done the Ruby way ... (I am a newbie...) > > here is an array > roles = [ "a", "b", "c", "d", "e" ] > > Depending upon a variable 'user.role', > I would like to suppress all elements in the array , less or equal to > this variable > > ex : > user.role = "a" #=> roles = ["b", "c", "d", "e" ] "a" deleted > user.role = "b" #=> roles = ["c", "d", "e" ] "a", "b" deleted > user.role = "c" #=> roles = [ "d", "e" ] "a", "b", "c" > deleted > user.role = "d" #=> roles = [ "e" ] "a", "b", "c", > "d" deleted > user.role = "e" # do nothing I'm not quite sure I understand your problem. Is this what you're looking for? roles = %w{a b c d e} user.role = "c" # non-destructive: roles.select{|role| role > user.role } #=> ["d", "e"] roles #=> ["a", "b", "c", "d", "e"] # destructive: roles.delete_if{|role| role <= user.role } roles #=> ["d", "e"] Cheers, Daniel