Ruby - 包括?当对象确实在数组中时返回false

时间:2014-04-14 03:05:47

标签: ruby arrays hash include

my_hash = Hash.new{|h,k| h[k] = []}
name = 'John'
my_hash[name] << '7'
my_hash[name] << '9'
name = 'Jane'
my_hash[name] << '7'
my_hash[name] << 'J'

array_info = my_hash.values_at('John')
puts array_info.class                   => array 
array_info.each do |ele|
   puts ele                             => 7, 9
end
removed = array_info.include?('7')
puts removed                           => false ??!?!?????

array_info刚刚打印7作为数组中的元素,因此我们知道它存在。那么为什么array_info.include?('7')不会返回true ??

1 个答案:

答案 0 :(得分:0)

看一下array_info。 Puts-it表明你的ele值确实在那个数组中,但是你的数组嵌套在另一个数组中。

removed = array_info.first.include?('7')

将返回true。