Reputation: 6012
print sentence.scan(/\[(\w+)\]/).all? do |word|
@words.keys.include? word[0].to_sym
end
I've printed the individual values of @words.keys.include? word[0].to_sym
and they are not all true, yet it prints true
. I think this might be because its evaluated like so:
(print sentence.scan(/\[(\w+)\]/).all?) do |word|
@words.keys.include? word[0].to_sym
end
but I want it to evaluate like
print (sentence.scan(/\[(\w+)\]/).all? do |word|
@words.keys.include? word[0].to_sym
end)
However, adding the parentheses results in
syntax error, unexpected keyword_do_block, expecting ')'
How do I change the order in which the piece of code is evaluated?
Edit:
I would like to return true instead of print unless the above is true, like:
@sentences.reject do |sentence|
!(sentence.scan(/\[(\w+)\]/).all? { |word| @words.keys.include? word[0].to_sym })
end
But it rejects everything.
Upvotes: 1
Views: 175
Reputation: 21791
This variant works for me:
print (sentence.scan(/\[(\w+)\]/).all?{|word| words.keys.include? word[0].to_sym })
Second variant:
print begin
(sentence.scan(/\[(\w+)\]/).all? do |word|
words.keys.include? word[0].to_sym
end)
end
Upvotes: 3
Reputation: 84343
You are creating pain for yourself in a couple of ways:
The argument to print is ambiguous. You should use print(...)
with no spaces.
Do/end binds less tightly than a {...}
block. Again, your example is somewhat ambiguous for the parser.
Try the less ambiguous syntax, and see if that clears things up before looking at alternative constructions.
Upvotes: 4