Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to use Enumerable

Reply
Thread Tools

How to use Enumerable

 
 
Luke Pearce
Guest
Posts: n/a
 
      09-25-2007
Hi,

Really need a little help understanding how to implement Enumerable -
maybe I'm thinking about this the wrong way - what I'd like to do is:

sm = ScoreMatrix.new
sm.keywords << Keyword.new(1)
sm.keywords << Keyword.new(2)
sm.keywords.sum #=> returns 3

I currently have:

class ScoreMatrix
def initialize
@keywords = [] # <= what do I put here?
end

def keywords
@keywords
end
end

class Keyword
include Enumerable

attr_accessor :hits

def initialize(hits)
@hits = hits
@array = []
end

def sum
total = 0
@array.each do |item|
total += item.hits
end
total
end

def each
@array.each do |item|
yield item
end
self
end

def add(item)
@array.push(item)
self
end
alias :<< :add
end

Am I on the right track or way off? :0)

Many Thanks

Luke
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Morton Goldberg
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 2007, at 2:21 PM, Luke Pearce wrote:

> Hi,
>
> Really need a little help understanding how to implement Enumerable -
> maybe I'm thinking about this the wrong way - what I'd like to do is:
>
> sm = ScoreMatrix.new
> sm.keywords << Keyword.new(1)
> sm.keywords << Keyword.new(2)
> sm.keywords.sum #=> returns 3
>
> I currently have:
>
> class ScoreMatrix
> def initialize
> @keywords = [] # <= what do I put here?
> end
>
> def keywords
> @keywords
> end
> end
>
> class Keyword
> include Enumerable
>
> attr_accessor :hits
>
> def initialize(hits)
> @hits = hits
> @array = []
> end
>
> def sum
> total = 0
> @array.each do |item|
> total += item.hits
> end
> total
> end
>
> def each
> @array.each do |item|
> yield item
> end
> self
> end
>
> def add(item)
> @array.push(item)
> self
> end
> alias :<< :add
> end
>
> Am I on the right track or way off? :0)


It looks to me that you are confusing a Class with an array of
objects of that class. It's hard for me to see what you are trying to
accomplish, but to the best of my understanding, all you need is
following:

<code>
class ScoreMatrix
attr_reader :keywords
def initialize
@keywords = []
end
def sum
@keywords.inject(0) { |s, keywd| s += keywd.hits }
end
end

class Keyword
attr_reader :hits
def initialize(hits)
@hits = hits
end
end

sm = ScoreMatrix.new
sm.keywords << Keyword.new(1)
sm.keywords << Keyword.new(2)
sm.sum # => 3
</code>

I can't see that you need to include Enumerable in the Keyword class.
You aren't treating Keyword objects as collections, you are just
iterating over an array of Keywords. Array already includes Enumerable.

Perhaps what you really want is for ScoreMatrix objects to behave as
if _they_ were an array of Keyword hits. If that's the case, then you
want something like:

<code>
class ScoreMatrix
include Enumerable
attr_reader :keywords
def initialize
@keywords = []
end
def each
@keywords.each { |keywd| yield keywd.hits }
end
def sum
inject(0) { |s, h| s += h }
end
end

class Keyword
attr_reader :hits
def initialize(hits)
@hits = hits
end
end

sm = ScoreMatrix.new
sm.keywords << Keyword.new(1)
sm.keywords << Keyword.new(2)
sm.sum # => 3
</code>

Regards, Morton

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
ActiveSupport Enumerable#sum should not use #size Trans Ruby 3 03-07-2008 11:07 PM
Basic question: how to use Enumerable#each_slice Liang He Ruby 1 10-13-2007 10:34 AM
Enumerable methods returning Enumerable (was: ruby-dev suumary 26862-26956) Daniel Sheppard Ruby 0 09-16-2005 02:40 AM
Why isn't Enumerable in StringIO? Jason Creighton Ruby 1 07-26-2003 05:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57