Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the Account and Portfolio classes to demo Enumerable

  • Loading branch information...
commit f916caa780a5eef63e04b0e18cf09f1f1d427f6f 1 parent 0e16524
Attila Domokos authored
Showing with 43 additions and 0 deletions.
  1. +13 −0 Iterator/account.rb
  2. +30 −0 Iterator/portfolio.rb
13 Iterator/account.rb
View
@@ -0,0 +1,13 @@
+class Account
+ attr_accessor :name, :balance
+
+ def initialize(name, balance)
+ @name = name
+ @balance = balance
+ end
+
+ def <=>(other)
+ balance <=> other.balance
+ end
+end
+
30 Iterator/portfolio.rb
View
@@ -0,0 +1,30 @@
+require 'account'
+
+class Portfolio
+ include Enumerable
+
+ def initialize
+ @accounts = []
+ end
+
+ def each(&block)
+ @accounts.each(&block)
+ end
+
+ def add_account(account)
+ @accounts << account
+ end
+end
+
+portfolio = Portfolio.new
+
+portfolio.add_account(Account.new('attila_account', 1000))
+portfolio.add_account(Account.new('john_doe_account', 400))
+portfolio.add_account(Account.new('jane_doe_account', 340))
+
+# check if any of the portfolio account's are greater than 2000
+puts portfolio.any? { |account| account.balance > 2000 }
+
+# check if all accounts have at least $10 in their balance
+puts portfolio.all? { |account| account.balance >= 10 }
+
Please sign in to comment.
Something went wrong with that request. Please try again.