Created
August 2, 2019 11:21
-
-
Save nafidurmus/0967dafdb7450529aecfeaa8b01100e7 to your computer and use it in GitHub Desktop.
This file contains 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 :name, :balance | |
def initialize(name, balance) | |
@name = name | |
@balance = balance | |
end | |
def <=>(other) | |
balance <=> other.balance | |
end | |
end | |
class Portfolio | |
include Enumerable | |
def initialize | |
@accounts = [] | |
end | |
def each(&block) | |
@accounts.each(&block) | |
end | |
def add_account(account) | |
@accounts << account | |
end | |
end | |
my_portfolio = Portfolio.new | |
my_portfolio.add_account(Account.new('Bonds', 200)) | |
my_portfolio.add_account(Account.new('Stocks', 100)) | |
my_portfolio.add_account(Account.new('Real Estate', 1000)) | |
my_portfolio.any? { |account| account.balance > 2000 } | |
my_portfolio.all? { |account| account.balance >= 10 } | |
my_portfolio.each { |account| puts "#{account.name}: #{account.balance}" } | |
my_portfolio.map { |account| account.balance } | |
my_portfolio.max | |
my_portfolio.minmy_portfolio = Portfolio.new | |
my_portfolio.add_account(Account.new('Bonds', 200)) | |
my_portfolio.add_account(Account.new('Stocks', 100)) | |
my_portfolio.add_account(Account.new('Real Estate', 1000)) | |
my_portfolio.any? { |account| account.balance > 2000 } | |
my_portfolio.all? { |account| account.balance >= 10 } | |
my_portfolio.each { |account| puts "#{account.name}: #{account.balance}" } | |
my_portfolio.map { |account| account.balance } | |
my_portfolio.max | |
my_portfolio.min |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment