首先进入到当前文件所在的,然后在命令行运行ruby 文件名
$ cd tmp/ruby$ruby demo01.rb
$ irbirb(main):001:0> print("hello")
exit可以退出当前的命令行
使用双引号中包含的转义字符会发生转义,使用单引号包含的字符通常不会发生转义,
但是\和单引号时,需要使用\去转义。
双引号
print("hello \n world! \n") #hello 和world会换行
单引号
print('hello \n world \n') #hello \n world \n
单引号发生转义
print('hello \\ \'world\' ') #hello \ 'world'
ruby在调用方法时可以省略(),print("hello“),其中print 是方法
“hello”为参数
print("hello") #等价 print "hello"
print 可以接受多个参数,参数间用逗号隔开
print "hello ","world ","aaa" #hello world aaa
print和puts
puts会每次打印的结果后面加换行符
puts "hello","world" #hello #world
p方法和print、 puts
p方法中数值和字符串会输出不同的结果,且转义字符不会发生转义
puts('20') #20puts(20) #20p('20') #'20'p(20) #20p("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=300print "面积为:#{area}mm" #面积为:300mm
单行注释使用#表示,从#到该行的结尾的内容都是注释的内容
#这是一行注释
多行注释 =begin和=end之间的内容
=begin作者:adsad年龄:阿斯大赛的=end
语法:if 条件 then 条件成立时执行 end
if 1==1 then print "1等于1" # 1等于1end
其中then可以省略
语法:if 条件 then 条件成立时执行 else 条件不成立时执行 end
if 1==2 then print "1等于2"else print "1不等于2" #1不等于2end
while语句
语法:while 循环条件 do 循环处理 end
其中do可以省略
eg: 打印1~10的数字
i=1while i<=10 do puts(i) i=i+1end
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] #helloarr[0]=12;puts arr[0] #12
给数组中不存在的索引赋值可以改变数组的长度
arr=["hello",12]arr[4]=10p arr[3] # nilputs arr[4] #10
获取数组的长度
arr=["hello",12]puts(arr.size) #2
遍历数组,对数组中的值执行某个方法
语法:数组.each do |n| 处理循环代码 end
eg:遍历数组,打印出数组中的每一个值
arr=["hello",12]arr.each do |n| puts nend
散列是程序中常用到的数据结构,散列一般使用字符串或者符号、数值作为健,来保存对应的对象
一般作为名称标签使用,创建符号,只需要在表示符或者字符串前加上:
sym=:foo #表示符号":foo"sym2=:"foo" #同上
符号和字符串转换
sym=:foop(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=>xiaohuduiend
创建正则表达式
regexp=/aaa/
用正则表达式匹配字符串
语法:/模式/ =~"字符串“
regexp=/aaa/reg=/java/puts regexp=~"lisadasaaadd" #7p 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.readprint textfile.close
执行ruby命令
$ruby 上面的脚本名 读取文件的文件名
逐行读取文件的内容,上面的程序的问题
一种更好的办法是逐行文件的内容
filename=ARGV[0]file=File.open(filename)file.each_line do |line| print lineendfile.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 endend
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(执行的结果)
联系客服