From: Josselin Date: 2006-08-11T15:40:05+09:00 Subject: Re: good writing in Ruby... On 2006-08-10 18:37:18 +0200, "Paolo Negri" said: > Some more infos about what are the relations among the User class and > the roles array would be nice. > > Anyway you can easily rewrite your role= method of the User class to > something like this > > def role=(value, array) > self.role = array.delete value if array.member? value > end > > or, if you are happy to end up with a role of nil in case the role is > invalid/already given away > > def role=(value, array) > self.role = array.delete value > end > > > On 10/08/06, 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 >> >> thanks for your tips >> >> Joss >> (I am on my way to buy the Ruby Recipes PDF Cookbook...) in this particular case, 'roles' array is built from the table 'roles', all values are possible roles for a new user so I don't modify the database this 'roles' array is presented as select options, 'roles' array content is based on current_user.role if current_user.role is 'god' then all collection (he can CRUD any role) is presented if current_user.role is 'admin' then all collection minus 'god' and 'admin' (an admin cannot create god users or admini users).... thanks