js

ruby进阶

2023-01-05  本文已影响0人  RickyWu585
1.object_id //200
1.object_id //200

"a".object_id //1200
"a".object_id //1220
Array.new(3,"abc") // 三个元素都是一个引用,改其中一个其他的都会变化
Array.new(3) {"abc"} // 不同引用,改其中一个不会影响其他
arr = [1,2,3,1,2]
arr.uniq // 去重,[1,2,3]
arr.last / arr[-1]  // 取最后一位
arr.shuffle // 打乱数组
[[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 // 去掉数组里的 nil
array.any? {|value| value > 3}  // 数组里是否存在大于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]
def method x
  return if x.blank? || x.empty?
  func1 ...
  func2 ...
end
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]
def foo
  p "foo"
end

alias :bar :foo
  1. 默认参数
  2. 任意多参数
def foo a,*b,c
  p a
  p b 
  p c
end

foo 1,2,3,4,5 
// 1  [2,3,4]  5
  1. hash参数:传入hash参数时,大括号可以省略
def foo hash
  h.each do |key,value|
    p [key,value]
  end
end

foo({a:1,b:2})
foo(a:1,b:2) // 传入hash参数时,大括号可以省略
// & 这个符号就相当于把block转换为proc,相当于一个方法变量
def foo(&block)
  a = 2
  block.call(a)
end

foo {|v| p v}

---------------------------------

arr = %w(a b c)
arr.map(&:capitalize)  // 这里相当于 capitalize这个method 转为 proc ,proc 再转为 block
  1. proc参数可以多传少传,lambda必须传准确的参数个数
  2. proc更像block,lambda更像method
  3. return
p = Proc.new {|x| return if x > 0}
p.call(1) // 报错

l = lambda {|x| return if x > 0}
l.call(1)  // 不会报错
  1. Class 可以看做一个 container of methods
  2. 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
    // 相当于 self.x > 0 && self.y > 0
    // 如果要修改实例变量的话,必须 @x = 1 或者 self.x = 1
  end

  def +(p2)
    Point.new(x + p2.x, y + p2.y)
  end

  // self在method上就是类方法,self在里面就是实例方法
  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  // 获取常量的方式
  1. 如果想继承一个方法,最好使用public或者protected,因为private也会被overload,可能造成不期望的后果
  2. 实例化对象只能调用public,protected和private只能在class内部调用
  3. 三者都可以被inheriate
  4. private 只能在内部隐式调用
class A
  def func1
    // 直接调用不会报错
    func1
    func2
  end 

  def func2
  end

  def func3
  end 

  protected :fun2
  private :fun3
end
  1. 在class内部,可以在obj上调用protected,不能调用private
class A
  def func1
    // self就是对应的实例化对象,可以call protected,不能call private
    self.func2 // 不会报错
    self.fun3  // 报错
  end

  def func2
  end

  def func3
  end 

  protected :fun2
  private :fun3
end

a = A.new
a.func1
  1. 类方法不能调用实例方法,只能调用类方法,实例方法中可以访问实例方法和类方法
  2. 类方法不能被overload,实例方法(三种)可以
  3. 类方法中不能引用实例变量,实例方法可以引用类变量和实例变量
  4. 类方法中不能super
module Helper
  def self.a
    p 'a'
  end

  def b
    p 'b'
  end
end

class P
  extend Helper
end

P.b // b
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
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
上一篇 下一篇

猜你喜欢

热点阅读