Ruby学习笔记

没有什么特别的原因开学学习Ruby,只是从兴趣出发

Programming Ruby 2nd Edition

Range

转化为数组
很简单,to_a方法搞定:
r = 3..12
arr = r.to_a     # [3,4,5,6,7,8,9,10,11,12]

digits = 0..9
digits.include?(5) -> true
digits.min -> 0
digits.max -> 9
digits.reject {|i| i < 5 } -> [5, 6, 7, 8, 9]
digits.each {|digit| dial(digit) } -> 0..9

 ('a'..'z').to_a.each{|i| puts i}   #显示a到z的字符串

r1 = 3..6
r2 = 3...6     #....不包含上线6
r1a, r1b = r1.first, r1.last    # 3, 6
r1c, r1d = r1.begin, r1.end     # 3, 6
r2a, r2b = r2.begin, r2.end     # 3, 6 (注意:不是3和5)

r1 = "7".."9"
r2 = "7".."10"
r1.each {|x| puts x }   # 打印出7,8,9
r2.each {|x| puts x }   # 未打印任何东西
##为什么会出现这样的情况?这是因为这里都是字符串,由于r1中,"7"比"9"小
所以,它是个合理的Range;而表达式r2中,"7"比"10"大,下限大于了上限,就不合理了。

r1 = 23456..34567
x = 14142
y = 31416
r1.include?(x)      # false
r1.include?(y)      # true

迭代器

3.times { print "X " }
1.upto(5) {|i| print i, " " }
99.downto(95) {|i| print i, " " }
50.step(80, 5) {|i| print i, " " }   #50至80之间步长5

数字

?a -> 97 #ASCII值

读取文件

f=File.open("testfile")
f.each{|line| puts line}
f.close

Reading

line=gets   #从键盘输入负值给line
print line  #打印line

Hash表

histogram = Hash.new() #新建一个Hash表  
histogram['key1'] -> 0 
histogram['key1'] = histogram['key1'] + 1
histogram['key1'] -> 1

histogram.default = "hello"  #没有定义的key会返回hello
   

Array

grep

grep方法调用===方法来匹配数组中的每一个元素与他自己所带的参数
由于是使用===方法,因此grep方法的参数不需要是正则表达式
a = %w[January February March April May]
a.grep(/ary/)      # ["January, "February"]
b = [1, 20, 5, 7, 13, 33, 15, 28]
b.grep(12..24)     # [20, 13, 15]

如果使用它的block版本的话,那就更灵活,这样可以再次处理所匹配的元素
# Continuing above example...
# Let's store the string lengths
a.grep(/ary/) {|m| m.length}     # [7, 8]
# Let's square each value
b.grep(12..24) {|n| n*n}         # {400, 169, 225}

sort

words = %w(the quick brown fox)       
list = words.sort  # ["brown", "fox", "quick", "the"]  #数组内部排序函数
puts list.to_s
# Or sort in place:
words.sort!        # ["brown", "fox", "quick", "the"]

sort方法假设数组里面没一个元素都是可比较的
因此比如[1, 2, "tHRee", 4]这样的数组,进行排序的话,就会报错. 

我们还可以使用一种更灵活的排序方式,那就是使用sort的block版本
a = [1, 2, "three", "four", 5, 6]
b = a.sort {|x,y| x.to_s <=> y.to_s}  #x,y分别是每次传进来的两个元素.
# b is now [1, 2, 5, 6, "four", "three"]
x = [1, 4, 3, 5, 2]
y = x.sort {|a,b| b <=> a}    # [5, 4, 3, 2, 1]

当我们想要根据很多key来排序的话,我们使用sort方法,就显得非常麻烦
这个时候我们能够使用Enumerable 模块的sort_by方法
words=[1,2,"3"]
puts words.sort       #错误不能排序
puts lists =words.sort_by{|item| item.to_s}   # 1 2 3 

shift

char=[a,b,b,c].shift   #删除数组中的第一个字符
puts char  # a

想得到数组中某几个元素组成的数组,你可以使用values_at方法

x = [10, 20, 30, 40, 50, 60]
y = x.values_at(0, 1, 4)        # [10, 20, 50]
z = x.values_at(0..2,5)         # [10, 20, 30, 60]
[1,3,5,7,9].find{|v| v*v < 30 }   #返回满足条件的第一个值
x.detect {|e| e % 2 == 0 }         # 8  功能与find相同

如果我们想得到这个数组中所有的偶数
这是就该使用find_all方法,而select和find_all方法是同意的
# Continuing the above example...
x.find {|e| e % 2 == 0}            # 8
x.find_all {|e| e % 2 == 0}        # [8, 12, 4, 30]
x.select {|e| e % 2 == 0}          # [8, 12, 4, 30]

["H", "A", "L"].collect {|x| x.succ }  #输下当前字符的下一字符

[1,3,5,7,9].each{|i| puts i }   #遍历数组

[1,3,5,7].inject {|sum, element| sum+element}                    #数组求和
[1,3,5,7].inject {|product, element| product*element}        #数组求积

a=Array.new  #新建一个数组

a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ]
a[0] -> "ant"
a[3] -> "dog"

# this is the same:
a = %w{ ant bee cat dog elk }
a[0] -> "ant"
a[3] -> "dog"

y = [1, 2, nil, nil, 3, 4]
c = y.size                 # 6
d = y.length               # 6
e = y.nitems               # 4   统计数组长度不包含nil

变量

全局变量 用$开头 实例变量 用@开头 类变量 用@@ 开头

person = "Tim"
person.id -> 936870
person.class -> String
person -> "Tim"

#{..} → 可以在引号中引用变量值或

def say_goodnight(name)
result = "Good night, #{name.capitalize}" #.capitalize输出字符串首字母大写
return result
end
puts say_goodnight('uncle')

输出结果:
Good night, Uncle

String

squeeze

我们可以使用squeeze 方法来一处重复的字符

s1 = "bookkeeper"
puts s2 = s1.squeeze         # "bokeper"
s3 = "Hello..."
puts s4 = s3.squeeze         # "Helo."
#If a parameter is specified, only those characters will be squeezed.
puts s5 = s3.squeeze(".")    # "Hello."

将一个字符串按照单词进行反转,那么你就会用到split方法和,数组的reverse方法

phrase = "Now here's a sentence"
puts phrase.split(" ").reverse.join(" ")
# "sentence a here's Now"

strip

strip将会删除掉字符串的开头和结尾的空格
sss="a d c "
puts sss.strip  => abc

如果你只想从字符串的开头或者结尾的空格,那么可以使用lstrip或者rstrip
str = "  abc  "
s2 = str.lstrip       # "abc  "
s3 = str.rstrip       # "  abc"

chop

chop方法将会删除最后一个字符,返回新的string。如果字符是以\r\n结束,则两个字符都会被删除

str = "abcxyz"
s1 = str.chop           # "abcxy"
str2="abc\r\n"  
s2=str2.chop  #abc

chomp

str = "abcxyz"
puts s1 = str.chomp #abcxyz
str2 = "123\n"
puts s2=str2.chomp   #123
str1 = "abcxyz"
str2 = "abcxyz"
puts s1 = str1.chomp("yz")   # "abcx"
puts s2 = str2.chomp("x")    # "abcxyz"   #只匹配结尾的词

unpack

puts  "E".unpack("c")    #69   输出一个字符串的asc码值

«

puts  "a"<<111    <<符号或者chr来把一个asc转换为字符串: 
puts  111.chr     

scan

"hello world".scan(/./){|s| print s}  #扫描匹配的字符串并打印

casecmp

n4 = "ABC".casecmp("abc")   # 0 功能同<=>忽略大小写

puts

str="bobo"
puts str.ljust(8,"++")    #左对齐其余用++补齐
puts str.rjust(8,"++")    #右对齐其余用++补齐
"Seconds/day: #{24*60*60}" -> Seconds/day: 86400
"#{'Ho! '*3}Merry Christmas!" -> Ho! Ho! Ho! Merry Christmas!
"This is line #$." -> This is line 3         # #?显示程序块中语句所在的行号

each_byte

str="bobo"  
str.each_byte{|byte| puts byte.chr}
结果:
b
o
b
o  

dup

person1 = "Tim"
person2 = person1.dup   #同.new功能相同也可以用person2=person1
person1[0] = "J"
person1 -> "Jim"
person2 -> "Tim"

freeze

person1 = "Tim"
person2 = person1
person1.freeze   # 字符串不可变
person2[0] = "J"
结果:
prog.rb:4:in `[]=': can't modify frozen string (TypeError)
from prog.rb:4

index & rindex

index方法返回指定的子字符串,正则表达式或者字符的起始位置(如果有多个匹配的只返回第一个匹配的起始位置)
没有发现的话返回nil,而rindex则是从string的右边(也就是结束处)开始查找,不过返回的值却是从左边数起的: 

str = "Albert Einstein"
puts pos1 = str.index(?E)        # 7
puts pos1 = str.index(69)        # 7
puts pos2 = str.index("bert")    # 2
puts pos3 = str.index(/in/)      # 8
puts pos4 = str.index(?e)        # nil
puts pos5 = str.index("bart")    # nil
puts pos6 = str.index(/wein/)    # nil

正侧表达式

sub或者gsub方法来进行替换,他们两个方法第一个参数都是接受正则表达式。
其中,sub方法替换掉第一个匹配的地方,而gsub方法替换掉全部匹配的地方: 

s1 = "spam, spam, and eggs"
s2 = s1.sub(/spam/,"bacon")               # "bacon, spam, and eggs"

s3 = s2.sub(/(\w+), (\w+),/,'\2, \1,')    # "spam, bacon, and eggs"

s4 = "Don't forget the spam."
s5 = s4.sub(/spam/) { |m| m.reverse }     # "Don't forget the maps."  把匹配部分的单词反转 
s5 = "alfalfa abracadabra"
s6 = s5.gsub(/a[bl]/,"xx")     # "xxfxxfa xxracadxxra"
s5.gsub(/[lfdbr]/) { |m| m.upcase + "-" }
# s5 is now "aL-F-aL-F-a aB-R-acaD-aB-R-a"

String类常用函数表

函数名称 说明 示例
* 将字符串拷贝N次 “ha”*4 » “hahahaha”
+ 连接字符串 “yes”+“no” » “yesno”
« 连接字符串 “yes”+“no” » “yesno”
concat 连接字符串 “yes”+“no” » “yesno”
比较字符串,返回值: 大于=-1 “Ab” ⇔ “ab” » -1
等于=0 “ab” ⇔ “ab” » 0
小于=1 “ab” ⇔ “Ab” » 1
== 或 ===判断两个对象是否相等 “1” == “1” » true , “1” == 1 » flase
=~匹配正则表达式 “abc123abc” =~ /\d/ » 3
[ ] 或 slice返回字符串的某一范围内的值 “abc”[0,2] » “ab”, “hello”[/llo/] » “llo”, “abc”.slice(0,2) » “ab”
[]=替换字符串的某一范围内的值a=“hello word” → a[1,2]=“OO” → puts a »“hOOlo word”
capitalize,capitalize!把字符串的首字母大写,其他字母小写“hi,Ruby”.capitalize » “Hi,ruby”
chomp,chomp!删除字符串后的空白字符“string”r”n”.chomp »“string”
chop删除最后一个字符“string”.chop » “strin”
count返回该字符串中含的字符个数a = “hello world” → a.count “lo” » 5 #(l出现 3次,o出现 2次)

基础

ruby打印输出命令

puts 带换行符输出

priint 不带换行符输出

ruby中区间表示

1..5表示1,2,3,4,5

1...5表示1,2,3,4

/home/yepnnet/public_html/wiki/data/pages/ruby.txt · 最后更改: 2008/10/12 09:33 由 admin
到顶部
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0