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. golang学习笔记11 golang要用jetbrain的golang这个IDE工具开发才好

    golang学习笔记11   golang要用jetbrain的golang这个IDE工具开发才好  jetbrain家的全套ide都很好用,一定要dark背景风格才装B   从File-->s ...

  2. AEC、AGC、ANS在视音频会议中的作用?

    AGC是自动增益补偿功能(Automatic Gain Control),AGC可以自动调麦克风的收音量,使与会者收到一定的音量水平,不会因发言者与麦克风的距离改变时,声音有忽大忽小声的缺点.ANS是 ...

  3. GUI常用对话框3

    %进度条 %waitbar h=waitbar(,'实例'); get(h); %获得进度条的子对象 get(get(h,'Children')) ha=get(h,'Children'); %获得坐 ...

  4. AtCoder Regular Contest 077 D - 11

    题目链接:http://arc077.contest.atcoder.jp/tasks/arc077_b Time limit : 2sec / Memory limit : 256MB Score ...

  5. [转载]FileStream读写文件

    FileStream读写文件 FileStream类:操作字节的,可以操作任何的文件 StreamReader类和StreamWriter类:操作字符的,只能操作文本文件. 1.FileStream类 ...

  6. 前端框架VUE----导入Bootstrap以及jQuery的两种方式

    Vue引入bootstrap主要有两种方法 方法一:在main.js中引入,此方法导入的bootstrap中对于html,body的一些预设置的css样式可能无效. 一.引入jQuery 在当前项目的 ...

  7. LINUX部署SVN服务器

    1.安装SVN服务端 yum install -y subversion 2.创建svn版本库 mkdir -p /data/svn/myproject svnadmin create /data/s ...

  8. PYQT5 + PYCHARM

    PYQT5 C:\Users\xxx\AppData\Local\Programs\Python\Python37\Lib\site-packages\pyqt5_tools\designer.exe ...

  9. linux下配置nginx使用ftp目录作为静态资源文件的目标目录

    1.安装ftp服务,可以直接yum install vsftpd. 2.设置随机启动,chkconfig vsftpd on. 3.启动ftp服务,service vsftpd start. 4.配置 ...

  10. 深入理解 Vue 组件

    深入理解 Vue 组件 组件使用中的细节点 使用 is 属性,解决组件使用中的bug问题 <!DOCTYPE html> <html lang="en"> ...