Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

HBGames

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?

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?

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top