1.irb参数配置
~/.irbrc
IRB.conf[:PROMPT_MODE] = :SIMPLE #简化 irb 提示符,以及禁用一些烦人的自动缩进行为
IRB.conf[:AUTO_INDENT_MODE] = false
2.注释:
#单行
=begin
......多行
=end
3.字符串
字符串连接
>> "jk" + "jk"
=> "jkjk"
字符串插值
>> one = "foo"
=> "foo"
>> "#{one} is one"
=> "foo is one"
字符串打印
>> puts "jk" #打印后自带一个换行符
jk
=> nil
>> p "jk" #打印出和原来一样的
"jk"
=> "jk"
>> print "jk" #打印不换行
jk=> nil
单双引号字符串
>> 'jk\n#{one}' #不进行转移字符和插值操作
=> "jk\\n\#{one}"
>> "jk\n#{one}" #输出转义后的字符和插值操作
=> "jk\nfoo"
对象和消息传送
>> "jk".length
=> 2
>> "".empty?
=> true
>> "jk".nil?
=> false
>> "jk".include?("j")
=> true
判断的时候可以配合 &&(和) || (或) !(非)来操作
>> one = "foo"
=> "foo"
>> two = ""
=> ""
>> if one.empty? && two.empty?
>> print "both empty"
>> elsif one.empty? || two.empty?
>> print "empty exist"
>> elsif !x.empty?
>> print "x is not empty"
>> end
empty exist=> nil
to_s可以把任何对象转换成字符串
>> nil.to_s.empty?
=> true
unless判断
>> two = "dasd"
=> "dasd"
>> print "ss" unless two.empty?
ss=> nil
使用!!可以两次取反,把对象转换为布尔类型
>> !!nil #nil对象为布尔假
=> false
>> !!0 #其他都为真
=> true
数值和范围
split方法,将字符串分割成想要的字符数组
>> "dasjkd das d dasd".split #默认去掉空格
=> ["dasjkd", "das", "d", "dasd"]
>> "fdjaskl,jfdskl,jfklasd".split(",") #设定用,分割
=> ["fdjaskl", "jfdskl", "jfklasd"]
数组的索引
>> a=[12,34,45,67,89,8]
=> [12, 34, 45, 67, 89, 8]
>> a[0]
=> 12
>> a[2]
=> 45
>> a[-4] #索引可是负数
=> 45
>> a.fifth #可以这样查找,但这是rails添加的效果
=> 89
>> a.first
=> 12
>> a.last
=> 8 >> a=[12,34,45,67,89,08] #注意数字之前不能有0,提示错误无效的十进制
SyntaxError: (irb):40: Invalid octal digit
a=[12,34,45,67,89,08]
^
数组的一系列方法
>> a.length
=> 6
>> a.include?(45)
=> true
>> a.sort #按有小到大顺序排列(不改变原有数组)
=> [8, 12, 34, 45, 67, 89]
>> a.reverse #顺序反过来
=> [8, 89, 67, 45, 34, 12]
>> a.shuffle #随意打乱顺序
=> [45, 12, 8, 34, 67, 89]
>> a.sort! #按有小到大顺序排列 (改变原有数组)
=> [8, 12, 34, 45, 67, 89]
>> a.push(4) #添加元素 (改变原有数组)
=> [8, 12, 34, 45, 67, 89, 4]
>> a << 3 #添加元素(改变原有数组)
=> [8, 12, 34, 45, 67, 89, 4, 3]
>> a << "tu" << "re" #串联添加,并且数组中可以添加其它类型的元素
=> [8, 12, 34, 45, 67, 89, 4, 3, "tu", "re"]
>> a.join #将字符串合并
=> "8123445678943ture"
>> a.join(",") #添加连接符
=> "8,12,34,45,67,89,4,3,tu,re"
范围操作
>> (0...9).to_a #去掉最后的一个值,转化为数组
=> [0, 1, 2, 3, 4, 5, 6, 7, 8]
>> (0..9).to_a #保留所有至
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
=> [0, 1, 2, 3, 4, 5, 6, 7, 8]
>> (0..9).to_a #保留所有至
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> a=(0..9).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> a[4..(a.length)] #使用length方法
=> [4, 5, 6, 7, 8, 9]
>> a[4..-2] #小技巧
=> [4, 5, 6, 7, 8]
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> a[4..(a.length)] #使用length方法
=> [4, 5, 6, 7, 8, 9]
>> a[4..-2] #小技巧
=> [4, 5, 6, 7, 8]
块
>> (1..9).each do |f|
?> print f*f
>> end
149162536496481
>> (1..9).map {|i| i*i} #将每个元素变换并返回map
=> [1, 4, 9, 16, 25, 36, 49, 64, 81]
>> (1..9).reduce {|i| i*i} #将整个数组变换为一个值返回
=> 1
>> %w{das das das} #%w将元素变换为字符串返回
=> ["das", "das", "das"]
哈希和符号
>> user = {} #初始化哈希
=> {}
>> user["one"] = "yz00"
=> "yz00"
>> user["two"] = "yz01"
=> "yz01"
=> {}
>> user["one"] = "yz00"
=> "yz00"
>> user["two"] = "yz01"
=> "yz01"
>> user["one"] #获取哈希
=> "yz00"
>> user
=> {"one"=>"yz00", "two"=>"yz01"}
=> "yz00"
>> user
=> {"one"=>"yz00", "two"=>"yz01"}
>> user = {"one" => "yz00","two" => "yz01"} #也可以这样赋值
=> {"one"=>"yz00", "two"=>"yz01"} #注意在逗号后面有一个空格,约定俗成的
=> {"one"=>"yz00", "two"=>"yz01"} #注意在逗号后面有一个空格,约定俗成的
可以把符号看成没有约束的字符串,所以可以写成下面的形式
>> user = {:one => "yz00", :two => "yz01"} #不用初始化
=> {:one=>"yz00", :two=>"yz01"}
由于符号当作键用的多,1.9后这样定义
>> user = {one: "yz00", two: "yz01"}
=> {:one=>"yz00", :two=>"yz01"}
=> {:one=>"yz00", :two=>"yz01"}
嵌套哈希
>> users[:user] = user
=> {:one=>"yz00", :two=>"yz01"}
>> users
=> {:user=>{:one=>"yz00", :two=>"yz01"}}
>> users[:user][:one]
=> "yz00"
>> print (1..5).to_a
[1, 2, 3, 4, 5]=> nil
>> puts (1..5).to_a
1
2
3
4
5
=> nil
>> puts (1..5).to_a.inspect #inspect方法输出数组的字面形式,输出对象时相当于P方法
[1, 2, 3, 4, 5]
=> nil
>> p (1..5).to_a
[1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]