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. Lucene 个人领悟 (二)

    想了想,还是继续写吧,因为,太无聊了,媳妇儿也还有半个小时才下班. 前面拖拖拉拉用了三篇文章来做铺垫,这一篇开始正经搞了啊. 首先,我要加几个链接 http://www.cnblogs.com/xin ...

  2. web前端利用turf.js生成等值线、等值面

    样例如下: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

  3. [转载]css代码优化的12个技巧

    1.ID 规则2.Class 规则3.标签规则4.通用规则对效率的普遍认识是从Steve Souders在2009年出版的<高性能网站建设进阶指南>开始,虽然该书中罗列的更加详细,但你也可 ...

  4. IDEA添加作者和时间信息

  5. STM32开发 -- 4G模块开发详解(转)

    STM32开发 -- 4G模块开发详解(1) STM32开发 -- 4G模块开发详解(2) STM32开发 -- 4G模块开发详解(3) STM32开发 -- 4G模块开发详解(4)

  6. MyBatis框架入门之(二)

    在本篇文章中,没有对细节进行处理的很好,有很多晓得细节的遗漏,本文只是一个简单的快速的入门 MyBatis的快速入门 导入MyBatis框架jar包 配置文件 SqlSessionFactoryBui ...

  7. JavaScript实现全选功能

    最终效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  8. python3 获取cookie解决方案

    python3 获取cookie解决方案 方案一: 利用selenium+phantomjs无界面浏览器的形式访问网站,再获取cookie值: from selenium import webdriv ...

  9. pagehelper调用mybatis报错java.lang.NoSuchMethodError:org.apache.ibatis.reflection.MetaObject.forObject

    最近在升新的基础框架到spring cloud,启动时pagehelper报错,如下: java.lang.NoSuchMethodError:org.apache.ibatis.reflection ...

  10. Kali系列之aircrack-ng wifi密码穷举

    kali linux安全渗透 网卡:rtl8187 工具aircrack-ng 操作+ 查看无线网卡信息 ifconfig 或者 iwconfig 有个是wlan0的就是您的外置无线网卡 启动网卡监听 ...