散列类
散列(Hash)和数组一样,都是表示对象集合的对象,不同之处在于,数组使用索引来取得元素,而散列则是用键key,key可以是任何对象,而索引则不能,只能是整数。
散列的创建
1.使用 {}
{key=>value}
或
{key:value}
Ruby中比较字符串是否相同也是用**==和!=**等运算符
1 | #whether two strings are equal |
如果是判断两字符串是否相似,采用正则表达式匹配更加简单。
1 | #whether two strings are similar |
Ruby中使用**” “或‘ ‘符号来创建字符串,在字符串中也可以包含表达式#{}**,这个表达式被称之为内嵌表达式。如果字符串中需要包含””或者’’等字符时,则可以通过转义字符\来实现。
1 | str = "this is a string" |
Octopress was broken after I upgraded Mac to OS X EI Capitan 10.11.1 in these days. The details of the problem is below.
Octopress is work well in Mac10.10. but when I upgraded to Mac10.11.1(OS X EI Capitan). And put rake preview in terminal to see the blog what looks like, got following error.
Starting to watch source with Jekyll and Compass. Starting Rack on port 4000
rake aborted!
Errno::ENOENT: No such file or directory - compass
/Users/user/git/octopress/Rakefile:85:in spawn
/Users/user/git/octopress/Rakefile:85:in block in <top (required)>
Tasks: TOP => preview
(See full trace by running task with --trace)
根据指定规则替换数组中的元素,Ruby也提供了以下方法:
ary.collect { |item| …}
ary.collect! { |item| …}
ary.map { |item| …}
ary.map! { |item| …}
将数组ary中的各元素item传递给块做处理。
1 | ary = [1, 2, 3] |