From: James Edward Gray II Date: 2006-08-11T01:27:42+09:00 Subject: Re: good writing in Ruby... On Aug 10, 2006, at 11:20 AM, Josselin wrote: > 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 See if this gives you some ideas: >> class User >> def initialize >> @roles = %w[a b c d e] >> @role = nil >> end >> ?> attr_accessor :role >> ?> def roles >> return @roles if @role.nil? or not (i = @roles.index(@role)) >> ?> @roles[0...i] >> end >> end => nil >> user = User.new => # >> user.roles => ["a", "b", "c", "d", "e"] >> user.role = "c" => "c" >> user.roles => ["a", "b"] James Edward Gray II