Ruby for Rails 最佳实践Ⅹ

来源:互联网 发布:如何使用万德数据库 编辑:程序博客网 时间:2024/05/19 07:44

第十章 标量对象

一、使用字符串

1. 字符串引用机制概要

记号

单引或双引

示例

输出

'

单引

'You\'ll have to "escape" single quotes.'

You’ll have to “escape” single quotes.

"

双引

"You'll have to \"escape\" double

quotes."

You’ll have to “escape” double quotes.

%q

单引

%q{'Single-quoted' example—no

escape needed.}

Single-quoted’ example—no escape needed.

%Q

双引

%Q{"Double-quoted" example—no

escape needed..}

“Double-quoted” example—no escape needed.

 

2. 字符串操作

(1)合并两个字符串

str = "a" + "b"

str = "a" << "b"

 

(2)替换字符串的内容

str = "Hi there."

str.replace("Good-bye.")

 

(3)改写字符串

 

方法

示例

结果

capitalize

upcase

downcase

swapcase

strip

lstrip

rstrip

chop

chomp

reverse

"ruby".capitalize

"cobol".upcase

"UNIX".downcase

"rUBY".swapcase

" lose the outer spaces "

" lose the left spaces "

" lose the right spaces "

"remove last character"

"remove training newline\n"

" gnirts eht esrever"

"Ruby"

"COBOL"

"unix"

"Ruby"

"lose the outer spaces"

"lose the left spaces "

" lose the right spaces"

"remove last characte"

"remove trailing newline"

"reverse the string"

 

注:所有这些方法都有相应的感叹号方法

 

4. 获取字符串的子字符串

字符串有一对读写方法:[]方法和[]=方法

(1)读方法[]

"abc"[2] # 输出:99

"abc"[2].chr # 输出:c

"abc"[-2].chr # 输出:b

"This is a string"[5,4] # 输出:is a

 

(2)写方法[]=

>> s = "This is a string."

=> "This is a string."

>> s[-1] = "!"

=> "!"

>> s

=> "This is a string!"

>> s[2,2] = "at"

=> "at"

>> s

=> "That is a string!"

 

5. 比较字符串

(1)比较两个字符串是否相等,最常用的方法 ==

"a" == "a" # 输出:true

"a" == "b" # 输出:false

另一种方法是 String#eql?,如 "a".eql?("a") # 输出:true

第三种方法是 String#equal?,如 "a".equal?("a") # 输出:false,因为两个字符串不是同一个对象

 

(2)字符串的比较与顺序

太空船方法/操作符(<=>)返回-1表示右边对象较大,1表示左边对象较大,0表示相等。

“a”比”A”要大,因为顺序是基于 ASCII 码值定义的。

 

二、符号及其用法

1. 符号是 Ruby 内建的 Symbol 的实例,它们的字面构造器是前导冒号

:a

:book

:"Here's how to make a symbol with spaces in it."

还可以通过对字符串调用 to_sym 方法(也可以用同意词 intern),以编程方式创建符号

>> "a".to_sym

=> :a

>> "Converting string to symbol with intern....".intern

=> :"Converting string to symbol with intern...."

也可以很容易将符号转换成字符串

>> :a.to_s

=> "a"

 

2. 符号与字符串的关键不同:对于任何给定的文本,只存在一个符号对象

>> :a.equal?(:a)

=> true

>> "a".equal?("a")

=> false

字符串与符号之间的另一个重要不同之处是:符号是不可变的,不能对符号的一部分进行增加、删除或改变。

符号在内存和处理时间上是高效的。相反,字符串具备的各种功能(如字符串变长,改变内容,等等)决定了其维护与处理的开销比较大。常常会见到符号被用作方法的参数,尤其是散列的键。

 

3. 重新讲解 Rails 风格的方法参数

class Work < ActiveRecord::Base

         belongs_to :composer

         # etc.

其中 :composer 是一个符号,它是 belongs_to 方法的参数。

<%= link_to "Click here",

             :controller => "book",

             :action     => "show",

             :id         => book.id %>

这是方法参数散列的一个例子,每一个符号都是散列的键,每一个右侧值都是散列的值。

 

三、数值对象

1. 在 Ruby 中,数是对象。和其它对象一样,可以给它们发送消息。

n = 98.6

m = n.round

puts m

x = 12

if x.zero?

         puts "x is zero"

else

         puts "x is not zero"

end

puts "The ASCII character equivalent of 97 is #{97.chr}"

 

2. 数值类层级结构

 

 

3. 常见算术表达式及其求值结果

表达式

结果

评注

1 + 1

10/5

10/3

10.0/3.0

1.2 + 3.4

-12 - -7

10 % 3

2

2

3

3.3333333333

4.6

-5

1

加法

整数除法

整数除法(无自动浮点数转换)

浮点数除法

浮点数加法

减法

模运算(余数)

 

4. 非十进制数运算

(1)十六进制数,前导 0x 指示

>> 0x12

=> 18

>> 0x12 + 12

=> 30

 

(2)八进制数,前导 0 指示

>> 012

=> 10

>> 012 + 12

=> 22

>> 012 + 0x12

=> 28

 

(3)用 to_i 方法将其它进制数转成十进制

>> "10".to_i(17)

=> 17

>> "12345".to_i(13)

=> 33519

>> "ruby".to_i(35)

=> 1194794

 

四、时间和日期

1. 时间和日期是通过三个类来操作的:Time、Date 和 DateTime。为了使用它们的全部功能,需要将 date 库或 time 库两者都请求加载到程序中或 irb 会话中

require 'date'

require 'time'

想知道英格兰是如何称呼1705年4月24日的吗?

>> require 'date'

=> true

>> Date.parse("April 24 1705").england.strftime("%B %d %Y")

=> "April 13 1705"

 

2. 获得系统当前时间

require 'date'

d = Date.today

puts d

2009-08-21

 

3. 日期对象可以响应 << 和 >> 方法

puts d << 2 # 输出:2009-06-21,两个月前的日期

puts d >> 5 # 输出:2010-01-21,五个月后的日期

 

4. 获得年、月、日、小时、分钟、秒及微秒

>> t = Time.new

=> Tue Jan 17 17:51:04 PST 2006

>> t.year

=> 2006

>> t.month

=> 1

>> t.day

=> 17

>> t.hour

=> 17

>> t.min

=> 51

>> t.sec

=> 4

>> t.usec

=> 377285

5. UNIX 风格的格式字符串

>> t.strftime("%m-%d-%Y")

=> "01-17-2006"

 

6. 常见的时间和日期格式说明符

说明符

描述

%Y

%y

%b, %B

%m

%d

%e

%a, %A

%H,%I

%M

%S

%c

%x

年份(4位数字)

年份(最后2位数字)

月份名的缩写、月份名的全称

月份(数字)

日子(左边补0)

日子(左边补空格)

星期几的缩写、星期几的全称

小时(24小时进制),小时(12小时进制)

分钟

等价于 %a %b %d %H:%M:%S %Y

等价于 %m/%d/%y

 

7. 一些时间格式说明符的用法示例

>> t.strftime("Today is %x")

=> "Today is 01/17/06"

>> t.strftime("Otherwise known as %d-%b-%y")

=> "Otherwise known as 17-Jan-06"

>> t.strftime("Or even day %e of %B, %Y.")

=> "Or even day 17 of January, 2006."

>> t.strftime("The time is %H:%m.")

=> "The time is 17:01."


原创粉丝点击