首先进入到当前文件所在的,而后在命令行运行ruby 文件名java
$ cd tmp/ruby $ruby demo01.rb
$ irb irb(main):001:0> print("hello")
exit能够退出当前的命令行正则表达式
使用双引号中包含的转义字符会发生转义,使用单引号包含的字符一般不会发生转义,
可是\和单引号时,须要使用\去转义。shell
双引号数组
print("hello \n world! \n") #hello 和world会换行
单引号ruby
print('hello \n world \n') #hello \n world \n
单引号发生转义数据结构
print('hello \\ \'world\' ') #hello \ 'world'
ruby在调用方法时能够省略(),print("hello“),其中print 是方法
“hello”为参数ui
print("hello") #等价 print "hello"
print 能够接受多个参数,参数间用逗号隔开编码
print "hello ","world ","aaa" #hello world aaa
print和puts
puts会每次打印的结果后面加换行符命令行
puts "hello","world" #hello #world
p方法和print、 puts
p方法中数值和字符串会输出不一样的结果,且转义字符不会发生转义code
puts('20') #20 puts(20) #20 p('20') #'20' p(20) #20 p("hello \n world") #"hello \n world"
Ruby的某些环境下,执行中文脚本会发生错误(invalid multibyte
char(utf-8)),这个是没有指定程序的编码形成的.
解决的方法:在程序的首行代码添加注释“# encoding:编码方式”,
若是没有魔法注释,默认使用utf-8
但愿以UTF-8编码在控制台输出结果,可使用-E 编码方式
$ruby -E UTF-8 脚本名称 $irb -E UTF-8
area=300 print "面积为:#{area}mm" #面积为:300mm
单行注释使用#表示,从#到该行的结尾的内容都是注释的内容
#这是一行注释
多行注释 =begin和=end之间的内容
=begin 做者:adsad 年龄:阿斯大赛的 =end
语法:if 条件 then 条件成立时执行 end
if 1==1 then print "1等于1" # 1等于1 end
其中then能够省略
语法:if 条件 then 条件成立时执行 else 条件不成立时执行 end
if 1==2 then print "1等于2" else print "1不等于2" #1不等于2 end
while语句
语法:while 循环条件 do 循环处理 end
其中do能够省略
eg: 打印1~10的数字
i=1 while i<=10 do puts(i) i=i+1 end
times方法
times用来处理一直循环次数的状况
语法:循环次数.times do 循环处理 end
eg:输出10次hello world
10.times do puts "hello world" end
建立数组
arr=[] #建立空数组 arr1=["hello",10] # 数组元素能够是不一样类型的对象 puts arr1[1] #10
经过索引访问和修改数组的值
arr=["hello",12] puts arr[0] #hello arr[0]=12; puts arr[0] #12
给数组中不存在的索引赋值能够改变数组的长度
arr=["hello",12] arr[4]=10 p arr[3] # nil puts arr[4] #10
获取数组的长度
arr=["hello",12] puts(arr.size) #2
遍历数组,对数组中的值执行某个方法
语法:数组.each do |n| 处理循环代码 end
eg:遍历数组,打印出数组中的每个值
arr=["hello",12] arr.each do |n| puts n end
散列是程序中经常使用到的数据结构,散列通常使用字符串或者符号、数值做为健,来保存对应的对象
通常做为名称标签使用,建立符号,只须要在表示符或者字符串前加上:
sym=:foo #表示符号":foo" sym2=:"foo" #同上
符号和字符串转换
sym=:foo p(sym.to_s) #“foo” str="foo" p str.to_sym #:foo
建立散列
song={ :title=>"love", :artist=>"xiaohudui" } #简洁写法 song1={ title:"love", artist:"xiaohudui" }
散列的使用
获取散列中的对象
song1={ title:"love", artist:"xiaohudui" } puts song1[:title] #love
给散列中添加对象
song={ title:"love", artist:"xiaohudui" } song[:tel]="121323213" p song #{:title=>"love", :artist=>"xiaohudui", :tel=>"121323213"}
遍历散列
语法:散列.each do |key,value| 循环处理代码 end
eg:遍历散列,打印出散列中全部的健和值
song={ title:"love", artist:"xiaohudui" } song.each do |key,value| print key,"=>",value,"\n" # title=>love # artist=>xiaohudui end
建立正则表达式
regexp=/aaa/
用正则表达式匹配字符串
语法:/模式/ =~"字符串“
regexp=/aaa/ reg=/java/ puts regexp=~"lisadasaaadd" #7 p reg=~"lisadasaaadd" #nil
匹配成功返回模式开始的位置,失败返回nil(表示对象不存在)
经过ARGV数组获取命令行中输入的参数
num1=ARGV[0] num2=ARGV[1] #使用to_i方法把字符串转化成整数 puts "num1+num2=#{num1.to_i+num2.to_i}" #num1+num2=3
运行ruby命令执行脚本
$ ruby 脚本名称 1 2
文件的读取
读取文件内容的流程:
filename=ARGV[0] file=File.open(filename) text=file.read print text file.close
执行ruby命令
$ruby 上面的脚本名 读取文件的文件名
逐行读取文件的内容,上面的程序的问题
一种更好的办法是逐行文件的内容
filename=ARGV[0] file=File.open(filename) file.each_line do |line| print line end file.close
each_line方法会对文件逐行读取,每次只读取一行的内容输出,知道文件的内容
输出完为之.
方法的定义和调用
#定义方法 def hello puts "hello" end #调用方法 hello #等价hello()
ruby中把能被其余程序引用的程序称为库,使用require或require_relative
方法来引用库,库名能够省略后缀名rb
调用require方法后,Ruby搜索指定库并读取指定库的内容,读取完毕后才会执行
require后面的内容。
require在预先定义好的路径下引用与ruby一块儿安装的库
require_relative是根据当前的脚本的执行目录来进行的
eg:模范grep命令的例子
grep.rb定义一个方法
def grep(pattern,filename) file=File.open(filename) file.each_line do |line| if pattern=~line then print line end end end
Uagegrep.rb中引用grep.rb
require_relative "grep" pattern=Regexp.new(ARGV[0]) filename=ARGV[1] grep(pattern,filename)
hello.txt
文件的读取 读取文件内容的流程: 1. 打开文件 2. 读取文件的内容 3. 输出文件的文本数据 4. 关闭文件 mzt
shell中输入
$ruby Uagegrep.rb mzt hello.txt #mzt(执行的结果)