From: dblack@... Date: 2006-12-02T06:06:10+09:00 Subject: Re: Simple iteration in a function problem Hi -- On Sat, 2 Dec 2006, Blake Miller wrote: > class Widget > > def initialize(title) > @components = Array.new > end > > def add_component(component) > @components << component > end > > def component(uniqueId) # note: uniqueId is a string > @components.find { |comp| comp.getUniqueId == uniqueId } > end > end > > ========================================================= > class SearchWidgetComponent > :attr_reader :uniqueId No : on attr_reader; it's a method: attr_reader :uniqueID > def getUniqueId > @uniqueId > end You've now got two "get" methods for @uniqueID: the one attr_reader created (which will be called uniqueID), and the one you've written. > end > > ============================= > class SearchWidgetComparisonComponent > > def getUniqueId > super super won't work here, unless this class is a subclass of SearchWidgetComponent. And if it is a subclass, it will have that method anyway. *And*... you don't need the method in the first place, because of attr_reader :-) > end > end Also, you need a way to set as well as get your uniqueID values, unless they're going to be something inherent in the object (the object's object_id). Do you want attr_writer too? David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org