Hi all,
In Rails console (just doing a general exercise in ruby design patterns), I create two classes:
ruby-1.9.2-p136 :023 > class A ruby-1.9.2-p136 :024?> attr_accessor :name, :balance ruby-1.9.2-p136 :025?> def initialize(name, balance) ruby-1.9.2-p136 :026?> @name = name ruby-1.9.2-p136 :027?> @balance = balance ruby-1.9.2-p136 :028?> end ruby-1.9.2-p136 :029?> def <=>(other) ruby-1.9.2-p136 :030?> balance <=> other.balance ruby-1.9.2-p136 :031?> end ruby-1.9.2-p136 :032?> end => nil ruby-1.9.2-p136 :033 > class Portfolio ruby-1.9.2-p136 :034?> include Enumerable ruby-1.9.2-p136 :035?> def initialize ruby-1.9.2-p136 :036?> @accounts = ruby-1.9.2-p136 :037?> end ruby-1.9.2-p136 :038?> def each(&block) ruby-1.9.2-p136 :039?> @accounts.each(&block) ruby-1.9.2-p136 :040?> end ruby-1.9.2-p136 :041?> def add_account(account) ruby-1.9.2-p136 :042?> @accounts << account ruby-1.9.2-p136 :043?> end ruby-1.9.2-p136 :044?> end => nil
client code: ruby-1.9.2-p136 :045 > portfolio = Portfolio.new => #<Portfolio:0x00000105d47118 @accounts=> ruby-1.9.2-p136 :091 > a = A.new('n',1000) => #<A:0x00000105fdc478 @name="n", @balance=1000> ruby-1.9.2-p136 :092 > portfolio.add_account(a) => [#<A:0x00000105fdc478 @name="n", @balance=1000>] ruby-1.9.2-p136 :093 > b = A.new('b',2000) => #<A:0x00000105fd0ba0 @name="b", @balance=2000> ruby-1.9.2-p136 :094 > portfolio.add_account(b) => [#<A:0x00000105fdc478 @name="n", @balance=1000>, #<A: 0x00000105fd0ba0 @name="b", @balance=2000>] ruby-1.9.2-p136 :095 > portfolio.each {|n, b| n <=> b } NoMethodError: undefined method `balance' for nil:NilClass
It appears that the each iterator does not support passing in two arguments? because I expect n to be object 0x00000105fdc478 and I expect b to be object 0x00000105fd0ba0. Then I expect once the each iterator of portfolio is called and executed, we in turn call each on the array of accounts, passing in our block. When that call occurs, we invoke the defined <=> instance method of A class, passing in the two objects stored in @accounts. This in turn will call the <=> method of enumerable and perform comparison operation. However, what I just described above does not occur.
thanks for response