p解析[“desc”] [“someKey”],在ruby中“p”是什么意思?

时间:2014-02-07 01:49:39

标签: ruby

# JSON Parsing example
require "rubygems"
require "json"

string = '{"desc":{"someKey":"someValue","anotherKey":"value"},"main_item":{"stats":{"a":8,"b":12,"c":10}}}'
parsed = JSON.parse(string) # returns a hash

p parsed["desc"]["someKey"]
p parsed["main_item"]["stats"]["a"]

# Read JSON from a file, iterate over objects
file = open("shops.json")
json = file.read

parsed = JSON.parse(json)

parsed["shop"].each do |shop|
  p shop["id"]
end

是预测吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

这是Kernel#p方法。

根据文件:

p(obj) → obj click to toggle source
p(obj1, obj2, ...) → [obj, ...]
p() → nil
     

对于每个对象,直接写入obj.inspect后跟换行符   该计划的标准输出。