@selections = []
sane = 0
while sane == 0 do
print "Make selections: "
myselections = gets.chop.split(" ")
myselections.each do |path| @selections.push(path)
end @selections.each do |selection|
choices.each do |choice|
if choices == selections then sane +=1
end
if sane != @selections.length then
puts "One of the selections is not available"
sane = 0 @selections = []
end
end
end
end
I need to make sure that myselections only contains elements from
choices, includes? doesn't work because it allows for partial matches.
This works but it looks absolutely horrible.
I'm not sure what 'partial matches' means in this context but maybe
this will be useful --
1.9.2-p290 :024 > choices = %w[a b c d e f g h i j k]
=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]
1.9.2-p290 :025 > selections = %w[c h k]
=> ["c", "h", "k"]
1.9.2-p290 :026 > bad_selections = %w[a b z]
=> ["a", "b", "z"]
1.9.2-p290 :027 > selections.drop_while{|selection| choices.include? selection }
=>
1.9.2-p290 :028 > bad_selections.drop_while{|selection|
choices.include? selection }
=> ["z"]
1.9.2-p290 :029 > selections.drop_while{|selection| choices.include?
selection }.count > 0
=> false
1.9.2-p290 :030 > bad_selections.drop_while{|selection|
choices.include? selection }.count > 0
=> true
I'm sure this is also one of those TMTOWTDI cases
···
On Tue, Apr 10, 2012 at 6:44 PM, ruby ismyname <lists@ruby-forum.com> wrote:
I need to make sure that myselections only contains elements from
choices, includes? doesn't work because it allows for partial matches.
This works but it looks absolutely horrible.
I need to make sure that myselections only contains elements from
choices, includes? doesn't work because it allows for partial matches.
This works but it looks absolutely horrible.
How about using array difference using the #- method?
choices = [1, 2, 3, 4]
=> [1, 2, 3, 4]
selection_good = [2, 4]
=> [2, 4]
selection_good - choices
=>
selection_bad = [2, 6]
=> [2, 6]
selection_bad - choices
=> [6]
HTH,
Ammar
···
On Wed, Apr 11, 2012 at 4:44 AM, ruby ismyname <lists@ruby-forum.com> wrote:
I would simply check the choice *before* pushing it into @choices:
···
####
UserError = Class.new StandardError
@selections = []
begin
invalid_choices = []
print "Make selections: "
myselections = gets.chop.split " "
myselections.each do |path|
if choices.include? path # I don't see any problems with include? @selections.push path
else
invalid_choices.push path
end
end
raise UserError if @selections.empty?
rescue UserError
puts 'No valid selections.'
retry
else
unless invalid_choices.empty?
puts "The following selections were invalid: #{invalid_choices.join
', '}"
end
end
####
good = Set[:a, :b, :d, :e]
good.subset? choices
=> true
bad = Set[:c, :f, :x, :y]
bad.subset? choices
=> false
Note: you can use Array#to_set to convert an array to a set.
···
On 04/11/2012 03:44 AM, ruby ismyname wrote:
I need to make sure that myselections only contains elements from
choices, includes? doesn't work because it allows for partial matches.
This works but it looks absolutely horrible.