用于设置转换的数组,返回具有十六进制值的集合

时间:2013-03-09 18:31:27

标签: ruby arrays set

我从文件中获取输入并将每行转换为数组,然后将该数组转换为集合。但是在转换时,集合会返回如下内容:

<Set:0x6268f8>

但是在IRB上运行相同的东西会返回正确的值。

require 'set'
n,p = gets.chomp.split.map { |e| e.to_i }
arr = gets.chomp.split( ).map{|x| x.to_i}
print arr
puts
old_set = arr.to_set
print old_set
if old_set.length != 1
    print "NO"
    exit
end

输入文件:

3 6
0 0 0 0 0 0
1 1 1 1 1 1
2 2 2 2 2 2

在运行时我得到:

C:\Ruby\kumar>ruby so.rb < abc.txt
[0, 0, 0, 0, 0, 0]
#<Set:0x3aad30>

在IRB上:

irb(main):010:0> arr = gets.chomp.split("")
aabbddefyy
=> ["a", "a", "b", "b", "d", "d", "e", "f", "y", "y"]
irb(main):011:0> se=arr.to_set
=> #<Set: {"a", "b", "d", "e", "f", "y"}>
irb(main):012:0> se
=> #<Set: {"a", "b", "d", "e", "f", "y"}>

1 个答案:

答案 0 :(得分:1)

输出#<Set:0x3aad30>表示结果是Set对象,hex值是该实例对象的内存地址。如果您想查看值,可以使用old_set.inspect执行此操作。您可以详细了解Set班级here