• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Ruby数据类型,变量

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  1. 整数类型: 3,222
  2. 小数: 3.14
  3. 字符串: hello,world
  4. 布尔类型: true(TrueClass),false(FalseClass)
  5. 数组: [1,2],["hello","hello world"]
  6. Hash(字典): {"name"=>"luo","age"=>24},{:name=>"daoyi",:age=>24}
  7. Symbol(符号)::a,:hello,:"hello world"
  8. Range: 1..10,1...10(三个点不包括10本身)
  9. 正则: /hello/i

String 字符串

a = "hello" #=> hello
b = "world" #=> world

a.class #=> String
a + " " + b #=> hello world
"#{a} #{b}" #=> hello world

string method

"hello world".length #=>11
"hello world".capitalize #=> Hello world
"hello world".gsub("world","gril").upcase #=> HELLO GIRL

变量赋值

a = "hello" # => hello
a.object_id # => 70353313681980

a.replace("hello2") # => "hello2"
a.object_id # => 70353313681980

a = "hello"
a.object_id # => 70353313549380

当a 使用replace时候仍然是原本的引用地址,所以Object_id不变
但是当a重新赋值为hello 的时候,a的引用地址发生了变化 object_id就改变了

以!结尾的方法

a = "hello" # => hello
a.capitalize # => Hello
a # => hello

a.capitalize! #=> Hello
a # => Hello
  1. !结尾的方法会改变变量资深
  2. 这只是个约束
  3. 在Rails中!的方法也被用来表示该方法会抛出异常

以?结尾的方法

a = "hello" # => hello
a.is_a?(String) #=> true
  1. ?的方法会返回true|false
  2. 这只是一个约束

nil

什么是nil

a = nil # => nil
a.nil? # => true

在ruby中nil和false都是"不成立"的意思,或者"否"的意思 其他一切都为true

双引号和单引号

a = "hello" # => hello
b = 'hello' # => hello

a == b # => true
c = "world" # => world
a = "hello #{c}" # => hello world
a = 'hello #{c}' # => hello \#{c}

双引号中的变量会被解释,单引号不会
反引号,直接运行shell命令

Array

a = [1,2,"hello"] # => [1,2,"hello"]
a.length # =>3

数组中可以放置任意类型

array常用方法

a = [1,2,"hello"]
a.length # =>3
a.size # =>3

a.first # =>1
a.last # =>hello

b = ["world"] # => ["world"]
c = a + b # => [1,2"hello","world"]
b *3 # => ["world","world","world"]

c - a # =>["world"]

这些方法并不会改变数组本身

a = [] # => []
a.push(1) # => [1]
a.push(2) # => [1,2]
a.unshift(3) # => [3,1,2]

a.pop # => 2
a.shift # => 3
a #=> 1

所有这些方法都会改变数组本身

Array 奇技淫巧

a = [] # =>[]
a << 3 # =>[3]

a.concat([4,5]) # => [3,4,5]
a.index(4) #=> 1
a[0] = 1 # => 1

a.max # => 5 

ruby中的方法

a = 1 # => 1
a + 2 # => 3 

在这里+ 只是一个方法,2是传递给方法的参数
可以解释为 a 拥有 +这样一个方法 2收传递给+方法的参数

Hash

a = {"name" => "luo","age"=>24,"hobbies" => ["coding","video game","music"]}
a["name"] # => "luo"

hash是无序的,数组是有序的

Hash 常用方法

a = {"name" => "luo","age"=>24,"hobbies" => ["coding","video game","music"]}

a.keys # => ["name", "age", "hobbies"]
a.values # => ["luo", 24, ["coding", "video game", "music"]]
a.delete("hobbies") # => ["coding", "video game", "music"]
a["cellphone"] = "16666666666" # => "16666666666"

Hash的其他定义方式

a ={name:"luo",age:24,hobbies:["coding","video game","music"]} 
# => {:name=>"luo", :age=>24, :hobbies=>["coding", "video game", "music"]}

Json的定义方式,顺应web前端的发展趋势

Symbol

a = :hello # => :hello
b = "#{a} world" # => "hello world"

c = :"hello world" # => :"hello world"
c.object_id # => 1160948
c = :"hello world" # => :"hello world"
c.object_id # => 1160948

Symbol是String的补充,可以看做为字符串来使用,但是Symbol和String在本质上还是不同的,在 Ruby中Symbol经常被用来作为hash的key和一些变化不频繁的字符串来使用

Symbol和hash

a ={:name => "luo",:age=>24,:hobbies =>["coding","video game","music"]}
a[:name] # => "luo"

Range

a = 1..10 # => 1..10
a.to_a.size # => 10

b = 1...10 # => 1...10
b.to_a.size # => 9
c = :a..:z # => :a..:z

d = a.to_a + c.to_a
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, :a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z]

Regular Expression 正则类型

a =/hello/ # => /hello/
"hello world" =~ a  # => 0

email_re = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i # => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
email_re.match("[email protected]") # => #<MatchData "[email protected]">

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
SASS的安装及使用(前提:安装Ruby)发布时间:2022-07-18
下一篇:
【AMAD】betamax--一个ruby-VCR的模仿品,只支持requests发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap