首先進入到當前文件所在的,然后在命令行運行ruby 文件名
$ cd tmp/ruby$ruby demo01.rb
$ irbirb(main):001:0> print("hello")
exit可以退出當前的命令行
使用雙引號中包含的轉(zhuǎn)義字符會發(fā)生轉(zhuǎn)義,使用單引號包含的字符通常不會發(fā)生轉(zhuǎn)義,
但是\和單引號時,需要使用\去轉(zhuǎn)義。
雙引號
print("hello \n world! \n") #hello 和world會換行
單引號
print('hello \n world \n') #hello \n world \n
單引號發(fā)生轉(zhuǎn)義
print('hello \\ \'world\' ') #hello \ 'world'
ruby在調(diào)用方法時可以省略(),print("hello“),其中print 是方法
“hello”為參數(shù)
print("hello") #等價 print "hello"
print 可以接受多個參數(shù),參數(shù)間用逗號隔開
print "hello ","world ","aaa" #hello world aaa
print和puts
puts會每次打印的結(jié)果后面加換行符
puts "hello","world" #hello #world
p方法和print、 puts
p方法中數(shù)值和字符串會輸出不同的結(jié)果,且轉(zhuǎn)義字符不會發(fā)生轉(zhuǎn)義
puts('20') #20puts(20) #20p('20') #'20'p(20) #20p("hello \n world") #"hello \n world"
Ruby的某些環(huán)境下,執(zhí)行中文腳本會發(fā)生錯誤(invalid multibyte
char(utf-8)),這個是沒有指定程序的編碼造成的.
解決的方法:在程序的首行代碼添加注釋“# encoding:編碼方式”,
如果沒有魔法注釋,默認使用utf-8
希望以UTF-8編碼在控制臺輸出結(jié)果,可以使用-E 編碼方式
$ruby -E UTF-8 腳本名稱$irb -E UTF-8
area=300print "面積為:#{area}mm" #面積為:300mm
單行注釋使用#表示,從#到該行的結(jié)尾的內(nèi)容都是注釋的內(nèi)容
#這是一行注釋
多行注釋 =begin和=end之間的內(nèi)容
=begin作者:adsad年齡:阿斯大賽的=end
語法:if 條件 then 條件成立時執(zhí)行 end
if 1==1 then print "1等于1" # 1等于1end
其中then可以省略
語法:if 條件 then 條件成立時執(zhí)行 else 條件不成立時執(zhí)行 end
if 1==2 then print "1等于2"else print "1不等于2" #1不等于2end
while語句
語法:while 循環(huán)條件 do 循環(huán)處理 end
其中do可以省略
eg: 打印1~10的數(shù)字
i=1while i<=10 do puts(i) i=i+1end
times方法
times用來處理一直循環(huán)次數(shù)的情況
語法:循環(huán)次數(shù).times do 循環(huán)處理 end
eg:輸出10次hello world
10.times do puts "hello world"end
創(chuàng)建數(shù)組
arr=[] #創(chuàng)建空數(shù)組arr1=["hello",10] # 數(shù)組元素可以是不同類型的對象puts arr1[1] #10
通過索引訪問和修改數(shù)組的值
arr=["hello",12]puts arr[0] #helloarr[0]=12;puts arr[0] #12
給數(shù)組中不存在的索引賦值可以改變數(shù)組的長度
arr=["hello",12]arr[4]=10p arr[3] # nilputs arr[4] #10
獲取數(shù)組的長度
arr=["hello",12]puts(arr.size) #2
遍歷數(shù)組,對數(shù)組中的值執(zhí)行某個方法
語法:數(shù)組.each do |n| 處理循環(huán)代碼 end
eg:遍歷數(shù)組,打印出數(shù)組中的每一個值
arr=["hello",12]arr.each do |n| puts nend
散列是程序中常用到的數(shù)據(jù)結(jié)構(gòu),散列一般使用字符串或者符號、數(shù)值作為健,來保存對應(yīng)的對象
一般作為名稱標簽使用,創(chuàng)建符號,只需要在表示符或者字符串前加上:
sym=:foo #表示符號":foo"sym2=:"foo" #同上
符號和字符串轉(zhuǎn)換
sym=:foop(sym.to_s) #“foo”str="foo"p str.to_sym #:foo
創(chuàng)建散列
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| 循環(huán)處理代碼 end
eg:遍歷散列,打印出散列中所有的健和值
song={ title:"love", artist:"xiaohudui"}song.each do |key,value| print key,"=>",value,"\n" # title=>love # artist=>xiaohuduiend
創(chuàng)建正則表達式
regexp=/aaa/
用正則表達式匹配字符串
語法:/模式/ =~"字符串“
regexp=/aaa/reg=/java/puts regexp=~"lisadasaaadd" #7p reg=~"lisadasaaadd" #nil
匹配成功返回模式開始的位置,失敗返回nil(表示對象不存在)
通過ARGV數(shù)組獲取命令行中輸入的參數(shù)
num1=ARGV[0]num2=ARGV[1]#使用to_i方法把字符串轉(zhuǎn)化成整數(shù)puts "num1+num2=#{num1.to_i+num2.to_i}" #num1+num2=3
運行ruby命令執(zhí)行腳本
$ ruby 腳本名稱 1 2
文件的讀取
讀取文件內(nèi)容的流程:
filename=ARGV[0]file=File.open(filename)text=file.readprint textfile.close
執(zhí)行ruby命令
$ruby 上面的腳本名 讀取文件的文件名
逐行讀取文件的內(nèi)容,上面的程序的問題
一種更好的辦法是逐行文件的內(nèi)容
filename=ARGV[0]file=File.open(filename)file.each_line do |line| print lineendfile.close
each_line方法會對文件逐行讀取,每次只讀取一行的內(nèi)容輸出,知道文件的內(nèi)容
輸出完為之.
方法的定義和調(diào)用
#定義方法def hello puts "hello"end#調(diào)用方法hello #等價hello()
ruby中把能被其他程序引用的程序稱為庫,使用require或require_relative
方法來引用庫,庫名可以省略后綴名rb
調(diào)用require方法后,Ruby搜索指定庫并讀取指定庫的內(nèi)容,讀取完畢后才會執(zhí)行
require后面的內(nèi)容。
require在預(yù)先定義好的路徑下引用與ruby一起安裝的庫
require_relative是根據(jù)當前的腳本的執(zhí)行目錄來進行的
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
文件的讀取讀取文件內(nèi)容的流程:1. 打開文件2. 讀取文件的內(nèi)容3. 輸出文件的文本數(shù)據(jù)4. 關(guān)閉文件mzt
shell中輸入
$ruby Uagegrep.rb mzt hello.txt #mzt(執(zhí)行的結(jié)果)