the ruby way iii

Post on 19-May-2015

425 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

The Ruby Way(3)

Meet a new world

luonet@2008.1

Ruby 常用库简介

Mixin 机制简介

Ruby 中的 Module 简介

Include 一个 Module

Extend 一个 Module

Enumerable 模块

Enumerable 模块

Enumerable 模块方法

判断集合中是否存在满足条件的元素 all? any?

集合的查询 find(detect) find_all

集合的查询 reject select

判断集合中是否存在某个元素 include? member?

集合的遍历 each_with_index

集合中元素的选取 max min

集合的排序 sort sort_by

集合的收集 collect(map) grep

其他有趣方法 inject partition

Array 类的常用方法

Array

Array#new

操作符方法 & |

操作符方法 * + -

操作符方法 <<

操作符方法 <=> ==

操作符方法 []

操作符方法 []=

遍历方法 each each_index reverse_each

元素获取 fetch first last

元素添加 insert push unshift

元素删除 delete delete_at delete_if

元素弹出 pop shift

数组的属性 length empty?

操作数组 clear compact!

操作数组 fill flatten

移除重复元素 uniq uniq!

数组的索引 index rindex

slice slice!

values_at

Hash 类的常用方法

new

== [] []=

new

default default=

遍历 each each_key each_pair each_value

判断键值的存在 has_key? has_value? key? value?

元素的获取 fetch

融合操作 merge merge!

String 类的常用方法

string

string 类上的操作符方法 %

操作符方法 * + <<

操作符方法 <=> ==

操作符方法 []

操作符方法 []=

大小写转换 capitalize downcase upcase swapcase

去除空白字符串 chomp lstrip rstrip strip

索引操作 index rindex

索引操作 index rindex

统计 count

插入 insert

删除 delete

子字符串操作 slice

字符串替换 sub gsub

字符串替换 tr tr_s

字符串匹配 match scan

字符串遍历 each each_line

split

squeeze

succ

upto

To be continued…i love ruby. And you?

top related