Hello to everyone who decides to read over this, and specially to those who can help :D
I've become stumped over an issue that I'm having while making a script, that I just can't seem to get past.
What I'm attempting to do is to check whether an array, let's call it array a contains all elements in array b. Sounds simple enough, right?
So, I attempted to do the logical thing, here, not sure if it would work, but it didnt hurt to try, right?
Of course, that didn't work, the expression would always evaluate false.
Now, the easiest thing to do here is to check each item, as so
However, in my script, array b may change size, therefore the code above wouldn't work anymore
The next thing I thought of doing would be a loop ...
And I believe that would work; however, I was thinking if it was possible to use the .each command here.
So I tried the following code
However, it didn't work, so my question is ... Is there some way to use the .each command for my problem, or am I just overcomplicating things, and should stay with the loop?
I've become stumped over an issue that I'm having while making a script, that I just can't seem to get past.
What I'm attempting to do is to check whether an array, let's call it array a contains all elements in array b. Sounds simple enough, right?
Code:
a = [1,2,3,4,5]
b = [1,3]
So, I attempted to do the logical thing, here, not sure if it would work, but it didnt hurt to try, right?
Code:
if a.include?(b)
p "yay"
end
Of course, that didn't work, the expression would always evaluate false.
Now, the easiest thing to do here is to check each item, as so
Code:
if a.include?(b[0]) and a.include?(b[1]
p "yay"
end
However, in my script, array b may change size, therefore the code above wouldn't work anymore
The next thing I thought of doing would be a loop ...
Code:
c = true
for i in 0...b.size
c = a.include?(b[i]) ? c : false
end
p "yay" if c
And I believe that would work; however, I was thinking if it was possible to use the .each command here.
So I tried the following code
Code:
if b.each {|x|a.include?(x)}
p "yay"
end
However, it didn't work, so my question is ... Is there some way to use the .each command for my problem, or am I just overcomplicating things, and should stay with the loop?