class BankAccount
@current_account_number = 0
def self.new(name)
super(name, @current_account_number += 1)
end
attr_accessor :name
attr_reader :account_number
def initialize(name, account_number)
@account_number = account_number
@name = name
end
end
More than 1 year has passed since last update.
【小ネタ】Ruby で new するたびにクラスインスタンス変数をインクリメントして、インスタンス変数にぶち込む
Last updated at Posted at 2021-08-14
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme