require "YAML"

var = YAML.load(File.open('b.yml'))

#哈希
puts var.class #Hash
print var ,"\n" #{"data"=>{"list"=>["Item1", "Item2", "Item3"], "hash"=>{"key1"=>"value1", "key2"=>"value2", "key3"=>"value3"}}} print var["data"].class,"\n" #Hash
print var["data"],"\n" #{"list"=>["Item1", "Item2", "Item3"], "hash"=>{"key1"=>"value1", "key2"=>"value2", "key3"=>"value3"}} print var["data"]["hash"].class,"\n" #Hash
print var["data"]["hash"],"\n" #{"key1"=>"value1", "key2"=>"value2", "key3"=>"value3"} #列表
print var["data"]["list"].class #Array
print var["data"]["list"] ,"\n" #Array["Item1","Item2","Item3"] #字符串和其他类型
=begin
YAML会自动判断类型。一般性的文字都会被解析成字符串。在有可能发生歧义的情况下,可为字符串加上单引号或者双引号
=end print var["data"]["str"].class,"\n" #Hash
print var["data"]["str"],"\n" #1=>1.0, 1.0=>"1.0", "1.0"=>#<Date: 2006-01-01 ((2453737j,0s,0n),+0s,2299161j)>} #字符串块
=begin
用| 表示保留换行符
用>表示删除换行符
=end
print var["data"]["strBone"].class,"\n"
print var["data"]["strBone"]
print var["data"]["strBone2"].class,"\n"
print var["data"]["strBone2"]
=begin
String
this is line1.
this is line2.
this is line3.
String
Hello, world.
=end # 完整的例子 对于重复出现的项目,可以用&进行定义,然后用*进行引用
var2 = YAML.load(File.open("abc.yml")) print var2.class,"\n" #Hash
print var2
=begin
{
"items"=>[
{"price"=>1.47, "quantity"=>4, "part_no"=>"A4786", "descrip"=>"Water Bucket (Filled)"},
{"price"=>100.27, "quantity"=>1, "part_no"=>"E1628", "descrip"=>"High Heeled /"Ruby/" Slippers"}
],
"bill-to"=>{"city"=>"East Westville", "street"=>"123 Tornado Alley/nSuite 16/n", "state"=>"KA"},
"specialDelivery"=>"Follow the Yellow Brick Road to the Emerald City. Pay no attention to the man behind the curtain.",
"date"=>#<Date: 4908637/2,0,2299161>,
"ship-to"=>{"city"=>"East Westville", "street"=>"123 Tornado Alley/nSuite 16/n", "state"=>"KA"},
"customer"=>{"given"=>"Dorothy", "family"=>"Gale"},
"logEvent"=>"Purchase Invoice"
}
=end 【b.yml】
data:
list:
- Item1
- Item2
- Item3 hash:
key1: value1
key2: value2
key3: value3 str:
1: 1.0
1.0: "1.0"
"1.0": 2006-01-01 strBone:
|
this is line1.
this is line2.
this is line3. strBone2:
>
Hello,
world.

【abc.yml】
logEvent: Purchase Invoice
date: 2018-07-12
customer:
given: Dorothy
family: Gale bill-to: &id001
street: |
123 Tomado Alley
Suite 16
city: East Westvile
state: KA ship-to: *id001 items:
- part_no: A476
descrip: Water Bucket(Filled)
price: 1.47
quantity: 4 - part_no: E1628
descrip: High Heeled "Ruby" Slippers
price: 100.27
quantity: 1 specialDelivery: >
Follow the yellow Brick
Road to the Emeraid City.
Pay no attention to the
man being the curtain.

【Ruby】【YAML】的更多相关文章

  1. 【Ruby】【基础】

    # [Ruby 块]=begin1 块由大量代码构成2 块中代码包含在{}内3 从与其相同名称的函数调用4 可以使用yield语句调用块=enddef test p '在test方法内' yield ...

  2. 【Ruby】【环境搭建】macOS Sierra 10.12.6 + Xcode 8 + gpg 2.2.8 + rvm 1.29.3 + Ruby 2.4.0 + RubyMine 2018.1.4

      按出场顺序: macOS Sierra 10.12.6  +  Xcode 8 + gpg 2.2.8 + rvm 1.29.3 +  Ruby 2.4.0 + RubyMine 2018.1.4 ...

  3. 【Ruby】【变量】

    知识点[Ruby 中$开头的全局变量.内部变量.隐藏变量介绍] Ruby 中充满了一系列的隐藏变量,我们可以从这些预定义的全局变量中获取一些有意思的信息. 全局进程变量 $$ 表示当前运行的 ruby ...

  4. 【Ruby】【遇到的问题】

    1 Error fetching https://gems.ruby-china.org/: certificate verify failed (https://gems.ruby-china.or ...

  5. 【Ruby】【高级编程】面向对象

    # [[面向对象]]#[实例变量]=begin实例变量是类属性,它们在使用类创建对象时就编程对象的属性.每个对象的属性是单独赋值的,和其他对象之间不共享.在类的内部,使用@运算符访问这些属性,在类的外 ...

  6. 【Web探索之旅】第四部分:Web程序员

    内容简介 1.第四部分第一课:什么是Web程序员? 2.第四部分第二课:如何成为Web程序员? 3.第四部分第三课:成为优秀Web程序员的秘诀 第四部分:Web程序员(完结篇) 大家好.终于来到了[W ...

  7. 【程序员小助手】Emacs,最强编辑器,没有之一

    内容简介 1.Emacs简介 2.Emacs三个平台的安装与配置 3.自动补全插件 4.小编的Emacs配置文件 5.常用快捷方式 6.和版本控制系统的配合(以SVN为例) [程序员小助手]系列 在这 ...

  8. 【GO】【gdb】

    1 安装homebrew 参考 https://www.cnblogs.com/suren2017/p/9249803.html ([Ruby][环境搭建]macOS Sierra 10.12.6 + ...

  9. 【精】搭建redis cluster集群,JedisCluster带密码访问【解决当中各种坑】!

    转: [精]搭建redis cluster集群,JedisCluster带密码访问[解决当中各种坑]! 2017年05月09日 00:13:18 冉椿林博客 阅读数:18208  版权声明:本文为博主 ...

  10. 【ELK】【docker】【elasticsearch】2.使用elasticSearch+kibana+logstash+ik分词器+pinyin分词器+繁简体转化分词器 6.5.4 启动 ELK+logstash概念描述

    官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-cli-run-prod ...

随机推荐

  1. 根据IP获取所在的国家城市

    根据IP获取所在的国家城市 新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:htt ...

  2. Git从远程仓库里拉取一条本地不存在的分支方法

    Git从远程仓库里拉取一条本地不存在的分支方法 从远程仓库里拉取一条本地不存在的分支时,进入到对应目录先执行git fetch然后再执行git checkout -b 本地分支名 origin/远程分 ...

  3. Codeforce 287A - IQ Test (模拟)

    In the city of Ultima Thule job applicants are often offered an IQ test. The test is as follows: the ...

  4. .NET 常用ORM之iBatis

    ibatis 一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目,到后面发展的版本叫MyBatis但都是指的同一个东西.最初侧重 ...

  5. MyBatis中的if写法

    <if test="latn_id !=null and latn_id !='' and latn_id !='100'"> and latnid=#{latn_id ...

  6. The Little Prince-11/26

    WRITE BEFORE THE BOOK REVIEW I have read The Little Prince for three or four times. However I still ...

  7. 金九银十跳槽季,程序员面试点解析之Java专场

    前言 近年来Java工程师这个岗位炙手可热,市场需求大,学习Java的人也越来越多,所以IT企业与求职者的选择都比较多,那么IT企业在面试时都会提哪些问题呢.下面为大家分享 Java高级工程师面试阿里 ...

  8. 2019/3/20 wen 流程控制

  9. 20145320《WEB基础实践》

    20145320WEB基础实践 实验问题回答 1.什么是表单 表单可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 一个表单有三个基本组成部分: 表单标签 表单域:包含了文本框.密码 ...

  10. 一、变量.二、过滤器(filter).三、标签(tag).四、条件分支tag.五、迭代器tag.六、自定义过滤器与标签.七、全系统过滤器(了解)

    一.变量 ''' 1.视图函数可以通过两种方式将变量传递给模板页面 -- render(request, 'test_page.html', {'变量key1': '变量值1', ..., '变量ke ...