Created
March 5, 2023 22:19
-
-
Save apaciuk/f456b168554dc20cae53437bfa8e596d to your computer and use it in GitHub Desktop.
Ruby self extending modules #modules #self extending #ruby #rails #object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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