lib/bindata/choice.rb in bindata-2.3.3 vs lib/bindata/choice.rb in bindata-2.3.4
- old
+ new
@@ -6,33 +6,33 @@
# at any particular time. Method calls will be delegated to the active
# choice.
#
# require 'bindata'
#
- # type1 = [:string, {:value => "Type1"}]
- # type2 = [:string, {:value => "Type2"}]
+ # type1 = [:string, {value: "Type1"}]
+ # type2 = [:string, {value: "Type2"}]
#
# choices = {5 => type1, 17 => type2}
- # a = BinData::Choice.new(:choices => choices, :selection => 5)
+ # a = BinData::Choice.new(choices: choices, selection: 5)
# a # => "Type1"
#
# choices = [ type1, type2 ]
- # a = BinData::Choice.new(:choices => choices, :selection => 1)
+ # a = BinData::Choice.new(choices: choices, selection: 1)
# a # => "Type2"
#
# choices = [ nil, nil, nil, type1, nil, type2 ]
- # a = BinData::Choice.new(:choices => choices, :selection => 3)
+ # a = BinData::Choice.new(choices: choices, selection: 3)
# a # => "Type1"
#
#
# Chooser = Struct.new(:choice)
# mychoice = Chooser.new
# mychoice.choice = 'big'
#
# choices = {'big' => :uint16be, 'little' => :uint16le}
- # a = BinData::Choice.new(:choices => choices, :copy_on_change => true,
- # :selection => lambda { mychoice.choice })
+ # a = BinData::Choice.new(choices: choices, copy_on_change: true,
+ # selection: -> { mychoice.choice })
# a.assign(256)
# a.to_binary_s #=> "\001\000"
#
# mychoice.choice = 'little'
# a.to_binary_s #=> "\000\001"
@@ -92,11 +92,11 @@
def method_missing(symbol, *args, &block) #:nodoc:
current_choice.__send__(symbol, *args, &block)
end
%w(clear? assign snapshot do_read do_write do_num_bytes).each do |m|
- self.module_eval <<-END
+ module_eval <<-END
def #{m}(*args)
current_choice.#{m}(*args)
end
END
end
@@ -147,14 +147,14 @@
end
result
end
def ensure_valid_keys(choices)
- if choices.has_key?(nil)
+ if choices.key?(nil)
raise ArgumentError, ":choices hash may not have nil key"
end
- if choices.keys.detect { |key| key.is_a?(Symbol) and key != :default }
+ if choices.keys.detect { |key| key.is_a?(Symbol) && key != :default }
raise ArgumentError, ":choices hash may not have symbols for keys"
end
end
end
@@ -172,13 +172,11 @@
obj.assign(prev) unless prev.nil?
remember_current_selection(current_selection)
end
def get_previous_choice(selection)
- if selection != @last_selection and @last_selection != nil
+ if @last_selection && selection != @last_selection
@choices[@last_selection]
- else
- nil
end
end
def remember_current_selection(selection)
@last_selection = selection