Ruby 学习笔记

Feb 8, 2017

Class variable

class Network
  def self.get(url, options)
    @@result = :test_result
  end
end

Instance variable

class Network
  def self.get(url, options)
    @result = :test_result
  end
end

Temporary attribute on ApplicationRecord

class Tester < ApplicationRecord
  attr_accessor :name  # for read and write
end

Method nameing

def available?
end

def update!
end

Class method

class User
  def self.validate!
  end
end

class User
  class << self
    def validate!
    end
  end
end

def User.validate!
end


User.validate!

Rewrite

class SubAccount < ApplicationRecord
  attr_reader :password  # provide instance variable with getter only

  def password=(val)
    self.password_digest = Digest::SHA1.hexdigest(val)
    @password = val
  end
end

[back]