[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

good writing in Ruby...

Josselin

8/10/2006 4:18:00 PM

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...)

17 Answers

Robert Klemme

8/10/2006 4:23:00 PM

0

On 10.08.2006 18:17, 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...)
>

Do you actually want to modify the array or do you just want to work
with the reduced set? Does this help?

>> roles = %w{a b c d e f}
=> ["a", "b", "c", "d", "e", "f"]
>> roles.select {|r| r >= "c"}
=> ["c", "d", "e", "f"]
>> roles.select {|r| r > "c"}
=> ["d", "e", "f"]
>> roles
=> ["a", "b", "c", "d", "e", "f"]
>> roles.delete_if {|r| r <= "c"}
=> ["d", "e", "f"]
>> roles
=> ["d", "e", "f"]

Kind regards

robert

Philip Hallstrom

8/10/2006 4:25:00 PM

0

James Gray

8/10/2006 4:28:00 PM

0

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:0x322b00 @role=nil, @roles=["a", "b", "c", "d", "e"]>
>> user.roles
=> ["a", "b", "c", "d", "e"]
>> user.role = "c"
=> "c"
>> user.roles
=> ["a", "b"]

James Edward Gray II


Paolo Negri

8/10/2006 4:37:00 PM

0

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 <josselin@wanadoo.fr> 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...)
>
>
>

Daniel Schierbeck

8/10/2006 6:00:00 PM

0

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

Chad Perrin

8/10/2006 8:02:00 PM

0

On Fri, Aug 11, 2006 at 01:20:08AM +0900, 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" ]

Do the "roles" have to be those letters, or did you just use them to
illustrate what you're doing? If you can use numbers for "roles"
instead, a whole new realm of possibilities opens up using comparison
operators.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
Ben Franklin: "As we enjoy great Advantages from the Inventions of
others we should be glad of an Opportunity to serve others by any
Invention of ours, and this we should do freely and generously."

Daniel Schierbeck

8/10/2006 8:57:00 PM

0

Chad Perrin wrote:
> On Fri, Aug 11, 2006 at 01:20:08AM +0900, 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" ]
>
> Do the "roles" have to be those letters, or did you just use them to
> illustrate what you're doing? If you can use numbers for "roles"
> instead, a whole new realm of possibilities opens up using comparison
> operators.

Actually,

"b" > "a" => true
"a" < "b" => true
"a" <=> "b" => -1


Cheers,
Daniel

Chad Perrin

8/11/2006 6:11:00 AM

0

On Fri, Aug 11, 2006 at 06:00:13AM +0900, Daniel Schierbeck wrote:
>
> Actually,
>
> "b" > "a" => true
> "a" < "b" => true
> "a" <=> "b" => -1

Silly me, I forgot Ruby did that.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"It's just incredible that a trillion-synapse computer could actually
spend Saturday afternoon watching a football game." - Marvin Minsky

Josselin

8/11/2006 6:23:00 AM

0

On 2006-08-10 22:56:32 +0200, Daniel Schierbeck
<daniel.schierbeck@gmail.com> said:

> Chad Perrin wrote:
>> On Fri, Aug 11, 2006 at 01:20:08AM +0900, 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" ]
>>
>> Do the "roles" have to be those letters, or did you just use them to
>> illustrate what you're doing? If you can use numbers for "roles"
>> instead, a whole new realm of possibilities opens up using comparison
>> operators.
>
> Actually,
>
> "b" > "a" => true
> "a" < "b" => true
> "a" <=> "b" => -1
>
>
> Cheers,
> Daniel

no, it was just for illustration.... roles are full string names...
'god' 'administrator' 'manager', 'owner', 'customer'
Initially I thought using an additional integer field as a 'priority'
'god' priority > 'administrator' priority > etc....

the objective is presenting a collection for select option depending
upon the current_user role
if 'god' then all collection (he can CRUD any role)
if 'admin' then all collection - 'god' and 'admin'
......

Josselin

8/11/2006 6:25:00 AM

0

On 2006-08-10 22:01:38 +0200, Chad Perrin <perrin@apotheon.com> said:

> On Fri, Aug 11, 2006 at 01:20:08AM +0900, 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" ]
>
> Do the "roles" have to be those letters, or did you just use them to
> illustrate what you're doing? If you can use numbers for "roles"
> instead, a whole new realm of possibilities opens up using comparison
> operators.

yeah see my answer to Daniel... I believe adding an integer priority
field will be easier (sure.. if I need to add new roles later...)
thanks