开发者

Any way to get the title or name of a hash and/or array in ruby

So pretty straight forward but I cant find the answer.

Say I have an object dumped in YAML as such:

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

I know from the dump that it is a hash by using .class and that the title is page. I can reference any of the keys as you normally would. How on earth do you get the title or name of the hash? and is it the same for arrays?


results of object.methods

Hash

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_se开发者_开发问答t
detect
values
to_xml
max
each_slice
inspect
[]
class_eval
present?
duplicable?
reverse_merge!
except
partition
delete
[]=
dup

keys and key cannot be referenced for some reason. observe

puts object.keys

returns

undefined method `keys' for nil:NilClass

puts object.key

returns

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

Solution: you guys are gonna hate this. I fixed the problem by running

sudo gem update

No joke. It updated ruby 1.8.7 to 1.8.7 and that was it. problem solved. I never done anything crazy with my ruby installation but it seems something in there must have messed with the hash. or perhaps it was a very rare bug. I cannot find anything in the update trace that would have caused this. Thanks to everyone who helped.


"page" is just a key in the top-level Hash. You could always just use Hash#keys to get the list of top-level names. In your case, it would be just one element, so:

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


Hashes and Arrays do not have "titles". Hashes have keys. :page is a key in the hash, and object[:page] is also a hash.

You can get all the keys of a hash by calling .keys

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

If you are trying to figure out if an object is a Hash or Array, you can call object.kind_of?(Hash), however you most likely shouldn't need to do this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜