Skip to content

Instantly share code, notes, and snippets.

@apaciuk
Created March 5, 2023 22:19
Show Gist options
  • Save apaciuk/f456b168554dc20cae53437bfa8e596d to your computer and use it in GitHub Desktop.
Save apaciuk/f456b168554dc20cae53437bfa8e596d to your computer and use it in GitHub Desktop.
Ruby self extending modules #modules #self extending #ruby #rails #object
class Account
attr_accessor :balance
def initialize(balance: 0.0)
@balance = balance
end
def deposit(amount)
@balance += amount
end
def withdraw(amount)
@balance -= amount
end
end
module BankTransaction
extend self
def deposit(account, amount)
account.deposit(amount)
end
def withdraw(account, amount)
account.withdraw(amount)
end
end
acc_1 = Account.new(balance: 100.00)
puts acc_1.balance
BankTransaction.deposit(acc_1, 1000.00)
puts acc_1.balance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment