- 相同數(shù)字的
object_id
不會(huì)變玄呛,相同字符串會(huì)變
1.object_id //200
1.object_id //200
"a".object_id //1200
"a".object_id //1220
- ruby interpreter(解釋器)將所有的
symbol
存放在一張symbol table
里匿垄,symbol很快,但是不會(huì)被垃圾回收
(garbage collecter)
Array.new(3,"abc") // 三個(gè)元素都是一個(gè)引用,改其中一個(gè)其他的都會(huì)變化
Array.new(3) {"abc"} // 不同引用溺忧,改其中一個(gè)不會(huì)影響其他
arr = [1,2,3,1,2]
arr.uniq // 去重融欧,[1,2,3]
arr.last / arr[-1] // 取最后一位
arr.shuffle // 打亂數(shù)組
[[1,2,3],[4,5]].flatten // [1,2,3,4,5]
arr.each {|value| p value}
arr.each_with_index {|value,index| p "#{index}: #{value}"}
arr.reverse_each {|value| p value}
arr.sort // 排序
arr.select {|value| value>3}
arr.compact // 去掉數(shù)組里的 nil
array.any? {|value| value > 3} // 數(shù)組里是否存在大于3的值
h = {a:1 , b:2}
h.assoc :b // [:b,2]
h.has_value? 2
h.has_key? :b
h.keys // [:a,:b]
h.values // [1,2]
h.to_a // [[:a,1],[:b,2]]
h.merge({c:3}) // {a:1 ,b:2, c:3}
h.each {|key,value| p [key,value]}
h.each_key {|key| p key}
h.each_value {|value| p value}
h.select {|key| key == :a} // {:a => 1}
h.delete a //刪除a
Set.new [1,2]
s = _
s.add 'foo'
b = Set.new [2,3]
s & b // {2},求交集
s | b // 求并集
s <= b // 求子集
r = 1..2 // [1,2]
r = 1...2 // [1,2)
r.include? 2 //true
a = [1,2,3,4]
a[1..2] // [2,3]
- ruby中如何讓 symbol 和 string 實(shí)現(xiàn)相同效果噪馏?麦到??
- 方法風(fēng)格:
在開(kāi)頭把所有會(huì)return的情況都走完
def method x
return if x.blank? || x.empty?
func1 ...
func2 ...
end
- 單例方法:
只對(duì)當(dāng)前對(duì)象有效
str = "hello"
def str.foo
p self
end
arr = [1,2,3]
def arr.+ num
self.dup << num
end
arr.+ 4 // [1,2,3,4]
- 方法別名:讓方法在不同環(huán)境下更加
語(yǔ)義化
def foo
p "foo"
end
alias :bar :foo
- 默認(rèn)參數(shù)
- 任意多參數(shù)
def foo a,*b,c
p a
p b
p c
end
foo 1,2,3,4,5
// 1 [2,3,4] 5
- hash參數(shù):傳入hash參數(shù)時(shí)欠肾,大括號(hào)可以省略
def foo hash
h.each do |key,value|
p [key,value]
end
end
foo({a:1,b:2})
foo(a:1,b:2) // 傳入hash參數(shù)時(shí)瓶颠,大括號(hào)可以省略
-
load
:如果在irb里load一個(gè)ruby文件,不僅可以跑一遍程序刺桃,還會(huì)把文件里的變量或者方法load到當(dāng)前irb環(huán)境里
- block里的return粹淋,指的是從包含這個(gè)block的方法return,如果這個(gè)block沒(méi)被方法包含瑟慈,是不能return的
- proc
// & 這個(gè)符號(hào)就相當(dāng)于把block轉(zhuǎn)換為proc桃移,相當(dāng)于一個(gè)方法變量
def foo(&block)
a = 2
block.call(a)
end
foo {|v| p v}
---------------------------------
arr = %w(a b c)
arr.map(&:capitalize) // 這里相當(dāng)于 capitalize這個(gè)method 轉(zhuǎn)為 proc ,proc 再轉(zhuǎn)為 block
- proc參數(shù)可以多傳少傳葛碧,lambda必須傳準(zhǔn)確的參數(shù)個(gè)數(shù)
- proc更像block借杰,lambda更像method
- return
p = Proc.new {|x| return if x > 0}
p.call(1) // 報(bào)錯(cuò)
l = lambda {|x| return if x > 0}
l.call(1) // 不會(huì)報(bào)錯(cuò)
- Class 可以看做一個(gè) container of methods
- Object is a receiver,that can respond to those methods
class Point
end
p = Point.new
p.methods(false) // 不顯示繼承的方法,只顯示父類的方法
class Point
attr_accessor :x,:y
def initialize x,y
@x,@y = x,y
end
def first_quadrant?
x > 0 && y > 0
// 相當(dāng)于 self.x > 0 && self.y > 0
// 如果要修改實(shí)例變量的話进泼,必須 @x = 1 或者 self.x = 1
end
def +(p2)
Point.new(x + p2.x, y + p2.y)
end
// self在method上就是類方法蔗衡,self在里面就是實(shí)例方法
def self.second_quradrant?(x,y)
x < 0 && y > 0
// 這里如果有self也是指類
end
// 批量定義class methods
class << self
def foo
end
def bar
end
end
@@origin = 0
// 類變量的getter必須自己定義?
def self.get_origin
@@origin
end
ORIGIN = 2
end
p1 = Point.new 1,2
p2 = Point.new 2,3
p1 + p2
Point.get_origin
Point::ORIGIN // 獲取常量的方式
- ruby中一個(gè)class的所有方法都可以被繼承
- public protected private
- 如果想繼承一個(gè)方法,最好使用public或者protected缘琅,因?yàn)閜rivate也會(huì)被overload粘都,可能造成不期望的后果
- 實(shí)例化對(duì)象只能調(diào)用public,protected和private只能在class內(nèi)部調(diào)用
- 三者都可以被inheriate
- private 只能在內(nèi)部隱式調(diào)用
class A
def func1
// 直接調(diào)用不會(huì)報(bào)錯(cuò)
func1
func2
end
def func2
end
def func3
end
protected :fun2
private :fun3
end
- 在class內(nèi)部刷袍,可以在obj上調(diào)用protected翩隧,不能調(diào)用private
class A
def func1
// self就是對(duì)應(yīng)的實(shí)例化對(duì)象,可以call protected,不能call private
self.func2 // 不會(huì)報(bào)錯(cuò)
self.fun3 // 報(bào)錯(cuò)
end
def func2
end
def func3
end
protected :fun2
private :fun3
end
a = A.new
a.func1
-
類方法不能調(diào)用實(shí)例方法
堆生,只能調(diào)用類方法专缠,實(shí)例方法中可以訪問(wèn)實(shí)例方法和類方法
- 類方法不能被overload,實(shí)例方法(三種)可以
- 類方法中
不能引用實(shí)例變量
淑仆,實(shí)例方法可以引用類變量和實(shí)例變量
- 類方法中不能super
- module只能將instance method mixin進(jìn)來(lái)涝婉,不能mixin class method
- module可以通過(guò)extend等方法將instance method mixin成class method
module Helper
def self.a
p 'a'
end
def b
p 'b'
end
end
class P
extend Helper
end
P.b // b
- 如果既想mixin instance method,又想mixin class method蔗怠,可以使用
included
這個(gè)hooks:
module Helper
def a
p 'a'
end
module ClassMethods
def b
p 'b'
end
end
// when include in klass, the hook is called
def self.included(klass)
klass.extend ClassMethods
end
end
class P
include Helper
end
P.included_modules // [Helper,Kernel]
P.include?(Helper) // true
p = P.new
p.a
P.b
- ruby可以理解為
沒(méi)有class method
墩弯,單例方法
存儲(chǔ)在對(duì)象的singleton_class中,是該對(duì)象的singleton_class
的instance method
寞射。因此extend本質(zhì)上就是把method include到class的singleton_class中
str = "hello"
def str.foo
'foo'
end
str.singleton_class
str.singleton_class.class // Class
str.singleton_methods // [:foo]
str.singleton_class.instance_methods(false) // [:foo]
class Foo
def self.foo
'foo'
end
end
Foo.singleton_methods // [:foo]
Foo.singleton_class.instance_methods(false) // [:foo]
Foo.singleton_methods == Foo.singleton_class.instance_methods(false) // true