Last active
August 29, 2015 13:56
-
-
Save nacengineer/9301156 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ irb | |
# sample array of slots | |
rbx-2.2.5 :001 > a = %w[slot1 slot3] | |
=> ["slot1", "slot3"] | |
# require our class, in this case called section | |
rbx-2.2.5 :002 > require './lib/section.rb' | |
=> true | |
# Now create a new Section Object with our slots above | |
rbx-2.2.5 :003 > s = Section.new slots: a | |
=> #<Section:0x48b0 @slots_mask=5> | |
rbx-2.2.5 :004 > s.slots | |
=> ["slot1", "slot3"] | |
# what is the slots_mask as an Integer? | |
rbx-2.2.5 :005 > s.slots_mask | |
=> 5 | |
# Now you can re-create any section with this slots_mask Integer value :) | |
rbx-2.2.5 :006 > s1 = Section.new slots_mask: 5 | |
=> #<Section:0x4954 @slots_mask=5> | |
rbx-2.2.5 :007 > s1.slots | |
=> ["slot1", "slot3"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Section | |
attr_accessor :slots, :slots_mask | |
def initialize(opts = {}) | |
opts.each {|k,v| self.send "#{k}=", v} | |
end | |
# Note that the order of this array is essentially locked. You should only add to the end of it! | |
SLOTS = %w(slot1 slot2 slot3 slot4) | |
def slots=(slots) | |
self.slots_mask = (slots & SLOTS).map {|r| 2**SLOTS.index(r)}.inject(:+) | |
end | |
def slots | |
SLOTS.reject {|r| ((slots_mask || 0) & 2**SLOTS.index(r)).zero?} | |
end | |
def slot_symbols | |
slots.map(&:to_sym) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment