Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Having some trouble in Ruby Essentials > Ruby Core > Enumerables

I've been following along with the code in Jason's video, but Ruby is throwing the following error:

bank_account.rb:33 in '<main>': undefined method 'each' for #<BankAccount:0x000000026d30d8> (NoMethodError)

The code I have pretty much copied exactly from the video:

class BankAccount attr_reader :transactions include Enumerable

def <=> (other_account)
    self.balance <=> other_account.balance
end

def initialize(name)
    @name = name
    @balance = 0
    @transactions = []
end

def deposit (amount)
    @transactions.push(amount)
end

def withdraw (amount)
    @transactions.push(-amount)
end

def balance
    @transactions.inject(0) {|sum,iterator| sum+=iterator}
end

end

account1=BankAccount.new("Jonathan Turnbull-Reilly") account1.deposit(100) account1.withdraw(50) account1.deposit(500) account1.withdraw(21) account1.each do |x| puts x end

Anyone have some thoughts that could help me?

Thanks!

Jonathan

4 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Jonathan,

Sorry for the trouble! Check out around 2:21 in this video where we add the each method:

http://teamtreehouse.com/library/programming/ruby-foundations/ruby-core/enumerable

Hey Jason (et al.)...I'm having some trouble fully 'grasping' this concept in its entirety. Can you dummy down what 'Enumeable' does in 2 sentences or less? Right now it is a vague and abstract concept floating around in my head which I know I will avoid going forward until I am forced to come to terms with it again...and I don't want that! O_O. Merci!

p.s. some real-world, plain english examples of when this module would be useful would be most appreciated!

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

I'll try :) Enumerable makes it easy to sort, compare, and modify classes and work with them more naturally and easily. In the BankAccount example, by defining the "spaceship" operator (<=>) that gives us the following options:

  • Sort bank accounts by the sum of transactions.
  • Find the maximum balance account.
  • Find the minimum balance account.

There's a ton of functionality that we can get "for free" by including the module and defining a few functions.

Please ask more questions! I feel like that may have made more than it answered :\

So.. if I understand correctly...

The Enumerable module holds a lot of methods that can be acted on a objects of a Class. But in order to make the enumerable module function, you need to set up the each method and the <=> method.

And we have seen enumerable methods on arrays and hashes before, but we just didn't have to set anything up, because the Array and Hash classes are already set up for us.

Am I getting it?