【发布时间】: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 - Get a Variable's Name][1] [1]: stackoverflow.com/questions/58482/ruby-get-a-variables-name
-
我想你要找的东西已经在这里得到了回答:[Ruby - Get a Variable's Name][1] [1]: stackoverflow.com/questions/58482/ruby-get-a-variables-name
-
@user688513 这个问题的答案有点晦涩,对我来说并没有真正的帮助。还是谢谢
标签: ruby-on-rails ruby arrays json hash