Your question is confusing, and your code isn't idiomatic Ruby. Consider writing it like:
new_array = []
new_array << 'nil'
new_array << 'nil2'
new_array << 'nil3'
loop do
load = gets.chomp
if new_array.include? load
puts 'That piece of the Array is loaded'
break
end
end
- We use snake_case_for_variables becauseItIsALotEasierToRead.
- While we can write
Array.new or Array.[], we seldom do. We usually assign [] to a variable instead.
- We typically
push to an array using <<. It's shorter to type, and visually sets apart what's happening.
- Use
loop do instead of while true.
I'd actually be more straightforward when defining that array:
new_array = %w[nil nil2 nil3]
And I'd use more mnemonic variable names so the code is more self-documenting:
ary = %w[nil nil2 nil3]
loop do
user_input = gets.chomp
if ary.include? user_input
puts 'That piece of the Array is loaded'
break
end
end
If you want to see if the value entered is part of a string element in the array:
if ary.any? { |s| s[user_input] }
puts 'That piece of the Array is loaded'
break
end
If you want to see if the value entered is the last character of a string element in the array:
if ary.any? { |s| s[-1] == user_input }
or:
if ary.any? { |s| s[/#{ user_input }$/] }
Read the documentation for any? to understand what it's doing.
loadis in the array OR ifloadis a substring of one of the elements in the array?part of array is loadedhe actually meansthat element exists.