From: Brian Candler Date: 2008-09-29T17:08:30+09:00 Subject: Re: irb: 1 & 1 => 1, not true huh? I thought 1 & 1 => tr > Bottom line is this: What is Ruby idiom to test for bit mask? Is there > a more simple way to write: > > if session[:permissions] & Permissions::SomeMask == > Permissions::SomeMask > # user has permission to wash clothes, etc. > end That's basically right - you can factor this out into your own method if you do it a lot. If you want to test for a single bit, there is Fixnum#[] a = 14 a[0] # 0 a[1] # 1 a[2] # 1 a[3] # 1 a[4] # 0 You'd still have to test for a[2] == 1, since 0 and 1 are both true. However you could use a different representation for your permissions, such as a string containing a single letter for each permission granted. Then: if session[:permissions].include?("w") # user has permission to wash clothes end or simply if session[:permissions]["w"] # user has permission to wash clothes end -- Posted via http://www.ruby-forum.com/.