【问题标题】:Any way to get the title or name of a hash and/or array in ruby在ruby中获取哈希和/或数组的标题或名称的任何方法
【发布时间】:2011-08-10 16:57:51
【问题描述】:

这么简单,但我找不到答案。

假设我在 YAML 中转储了一个对象:

page: 
  page_url: http://www.stackoverflow.com/
  created_at: "2011-08-10T11:00:19Z"
  website_id: 1
  updated_at: "2011-08-10T11:00:19Z"
  id: 1
Hash

我从转储中知道它是使用 .class 的哈希,并且标题是页面。我可以像往常一样引用任何键。您到底是如何获得哈希的标题或名称的?数组也一样吗?


object.methods 的结果

哈希

presence
zip
find_index
invert
silence_stderr
enum_slice
deep_merge
load
gem
require_dependency
extract!
minmax
encode64
rehash
debugger
methods
send
assert_valid_keys
replace
values_at
empty?
group_by
diff
taint
instance_variable_defined?
shift
stringify_keys
take
to_param
to_yaml
key?
find_all
update
silence_stream
as_json
singleton_class
keys
require
many?
min_by
each_cons
entries
to_hash
singleton_methods
instance_eval
require_library_or_gem
to_json
first
require_or_load
nil?
b64encode
take_while
select
deep_merge!
with_options
extractable_options?
exclude?
max_by
enum_cons
deep_symbolize_keys
protected_methods
instance_exec
display
sort
store
returning
tainted?
method
delete_if
include?
stringify_keys!
untaint
instance_of?
merge!
symbolize_keys
drop
equal?
suppress
reject
merge
minmax_by
sum
to_set
yaml_initialize
hash
private_methods
all?
sort_by
default_proc
to_query
silence_warnings
`
require_association
kind_of?
freeze
acts_like?
drop_while
eql?
indexes
html_safe?
collect
id
reverse_merge
slice
reject!
grep
each_value
instance_values
with_warnings
any?
public_methods
breakpoint
symbolize_keys!
object_id
is_a?
has_key?
cycle
indices
to_options
except!
map
member?
tap
type
fetch
with_indifferent_access
each_with_index
decode64
count
each_key
instance_variable_names
enable_warnings
one?
instance_variables
try
__id__
frozen?
suppress_warnings
clear
load_dependency
to_enum
to_yaml_properties
has_value?
to_a
respond_to?
each_with_object
each
taguri
inject
slice!
class
encode_json
reverse_update
length
enum_with_index
instance_variable_get
each_pair
copy_instance_variables_from
none?
taguri=
find
==
__send__
===
enum_for
min
to_options!
extend
value?
to_s
default
index_by
index
size
reduce
blank?
unloadable
to_yaml_style
default=
clone
reverse_each
decode_b
=~
instance_variable_set
detect
values
to_xml
max
each_slice
inspect
[]
class_eval
present?
duplicable?
reverse_merge!
except
partition
delete
[]=
dup

keys 和 key 由于某种原因不能被引用。观察

puts object.keys

返回

undefined method `keys' for nil:NilClass

puts object.key

返回

undefined method `key' for #<Hash:0x103aeb0b0>

解决方案:你们会讨厌这个。我通过运行解决了问题

sudo gem update

不开玩笑。它将 ruby​​ 1.8.7 更新到 1.8.7,仅此而已。问题解决了。我从来没有对我的 ruby​​ 安装做过任何疯狂的事情,但似乎里面一定有什么东西弄乱了哈希值。或者也许这是一个非常罕见的错误。我在更新跟踪中找不到任何会导致此问题的内容。感谢所有提供帮助的人。

【问题讨论】:

标签: ruby-on-rails ruby arrays json hash


【解决方案1】:

“page”只是顶级Hash中的一个key。您总是可以使用Hash#keys 来获取顶级名称列表。在你的情况下,它只是一个元素,所以:

x.keys.first == 'page'
=> true

【讨论】:

  • #<0x103aeb0b0>
【解决方案2】:

哈希和数组没有“标题”。哈希有键。 :page 是 hash 中的 key,object[:page] 也是 hash。

您可以通过调用 .keys 来获取哈希的所有键

object.keys
=> [:key1, :key2, :key3]

如果您想确定一个对象是哈希还是数组,您可以调用 object.kind_of?(Hash),但您很可能不需要这样做。

【讨论】:

  • 我不需要弄清楚它们是数组还是散列。长话短说,我有一个 JSON 字符串,我已将其转换为一个包含多个其他对象的数组。但是当我尝试根据他们的名字检查他们是什么类型的对象时,我没有什么可参考的
  • 另外,object.keys 作为一个未定义的方法回来了,这很奇怪。
猜你喜欢
  • 2014-01-22
  • 1970-01-01
  • 2010-11-26
  • 2014-10-05
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 2010-09-19
  • 1970-01-01
相关资源
最近更新 更多