翔如菲菲

其实天很蓝,阴云总会散;其实海不宽,此岸连彼岸.

导航

Ruby编程语言学习笔记3

对应Ruby编程语言第三章

=begin
Ruby包含了5个表示数字的内建类:Numeric Float Integer Fixnum Bignum
及标准库中的3个数值类:Complex BigDecimal Rational  
             Numeric
                |
   _______________________________________
   |         |       |          |         |
 Integer Float   Complex  BigDecimal Rational
   |
 ___________
 |         |
 Fixnum  Bignum       

Ruby中的所有数字对象都是Numeric类的示例,所有的整数都是Integer的实例,
Bignum和Fixnum是透明转换的(如果一个Fixnum对象,其结果值超出Fixnum范围,则透明转换为Bignum,反之亦然)
=end

#整数字面量
#
可以在整数字面量里插入下划线,此特性被称为千分符
print 1_000_000,"\n" #表示100万
#
0开头的是8进制,0x或者0X开头的是16进制,0b或者0B开头的是二进制数
print 017,"\n"         #八进制,输出十进制15(1*8+7=15)
print 0377,"\n"        #八进制,输出十进制255(3*8*8+7*8+7=255)
print 0b1111_1111,"\n" #二进制 输出255(128+64+32+16+8+4+2+1=255)
print 0xFF,"\n"        #十六进制 输出255(15*16+15=255)

#浮点数字面量
print 6.02e2,"\n"  #602.0
#
求余操作
print 5%2,"\n"     #1
print 1.5%0.4,"\n" #0.3

print 5/-2,"\n"     #1
print -5/2,"\n"     #1

#divmod method return a array for storing quotient(商) and remainder(余数)
p 10.divmod(3) #=>[3, 1]

 

#remainder method remainder which is the same as dividend Positive or negative
p 10.remainder(3.5) #=>3.0
p 10.remainder(-3.5) #=>3.0
p -10.remainder(3.5) #=>-3.0
p -10.remainder(-3.5) #=>-3.0

#modulo method is the same as % operation
p 10.modulo(3) #=>1

#n.times block :repeat n time to execute block
array=[]
10.times do
  |i|
  array<<i
end
p array #=>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

#start.upto(end) block : loop start to end with 1 as increment factor
array=[]
2.upto(10){|i| array<<i }
p array #=>[2, 3, 4, 5, 6, 7, 8, 9, 10]

#start.downto(end) block : loop start to end with 1 as decrement factor
array=[]
10.downto(2){ |i| array<<i }
p array #=>[10, 9, 8, 7, 6, 5, 4, 3, 2]

#start.upto(end) block : loop start to end with step as increment factor
array=[]
2.step(10,2){ |i| array<<i }
p array #=>[2, 4, 6, 8, 10]

 

#Hashes(哈希)

hash=Hash.new
hash["one"]=1
hash["two"]=2
puts hash     #{"one"=>1, "two"=>2}

hash2={}
hash2["one"]=2
hash2["two"]=4
puts hash2    #{"one"=>2, "two"=>4}

#Hash Literals(哈希字面量)
  #哈希字面量就是:包含在花括号中由逗号分隔的一列键值对
numbers={"one"=>1,"two"=>2,"three"=>3,4=>8}
puts numbers  #{"one"=>1, "two"=>2, "three"=>3, 4=>8}

  #作为哈希的键,Symbol对象比字符串更高效,Symbol是不可变的,功能受限的字符串,以冒号为前缀的标识符:如 :string,:one
numbers={:one=>1,:two=>2,:three=>3,:f4=>8}
puts numbers  #{:one=>1, :two=>2, :three=>3, :f4=>8}

  #Ruby1.9中,是由Symbol作为键时,可以用如下的简洁方式来构建哈希字面量
numbers={one:1,two:2}
puts numbers  #{:one=>1, :two=>2}
#
Ranges(范围)
  #Ranges表示位于一个开始值和一个结束值之间的值,起始值和终止值中间放置2个点(包含终止值)和3个点(不包含终止值)
r=1..10
r.each{|item| print item ,' '}  #1 2 3 4 5 6 7 8 9 10
r=1...10
r.each{|item| print item ,' '}  #1 2 3 4 5 6 7 8 9
 
  #可以用include? 来表示一个值是否被包含在ranges内
puts r.include? 8 #true

#Symbol(符号)
#
一个Symbol对象引用一个符号,Symbol是不可变的,功能受限的字符串,以冒号为前缀的标识符

sym=:"Symbol demo"
puts sym #Symbol demo

s="Symbol demo"
sym=:"#{s}"
puts sym #Symbol demo

#Ruby解释器会维护一个符号表(symbol table),它存储了其所知晓的所以类、方法及变量的名称。
#
在反射代码(reflective code)里,符号常常被用于引用方法名,比如想知道某个对象是否包含each方法
o=Object.new
puts o.respond_to? :each  #false

#你可以使用intern或者to_sym方法讲一个String转换为一个Symbol
#
可以使用to_s方法或者id2name讲一个Symbol转换回String
str="This is string"
sym=str.intern
sym=str.to_sym
puts sym  #This is string
str2=sym.to_s
str2=sym.id2name
puts str2 #This is string
#
2个字符串包含相同的内容,但是他们可以是2个不同的对象,
#
对于内容相同的字符串,转换成Symbol时 总是指向同一个Symbol对象

#True False Nil
#
true false 是2个布尔值,代表了真和假,nil是特殊的保留值,用于表示没有值
#
如果想检测一个值是否为nil,可以用如下的方式:
#
1.和nil进行比较
o==nil
#2.用nil?方法
o.nil?
#true false nil引用的都是对象不是数值,它们分别对应的是TrueClass,FalseClass,NilClass类的实例

#Ruby需要一个布尔值时,nil表现为false,nil和false之外的所有制都表现为true 

posted on 2012-06-06 16:21  翔如飞飞  阅读(280)  评论(0编辑  收藏  举报