From: Blake Miller Date: 2006-12-02T06:33:10+09:00 Subject: Re: Simple iteration in a function problem unknown wrote: > Hi -- > > On Sat, 2 Dec 2006, Blake Miller wrote: > >> 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 Noted, and changed. Sorry if I'm dragging this out, I know I left out a couple details in the code I showed. Indeed SearchWidgetComparisonComponent does, and has been extending SearchWidgetComponent. My new code is like this: class SearchWidget def initialize @components = Array.new end def add_component(component) @components << component # these will always be subclass of SearchWidgetComponent end def printcomps # i call this from index.rhtml, and it prints correctly the 2 *different* component ids str = "" @components.each_with_index do |comp, index| str += @components[index].uniqueId end str end def component(uniqueId) @components.find { |comp| comp.uniqueId == uniqueId } end end ================================== class SearchWidgetComponent attr_accessor :uniqueId def initialize(label, uniqueId) # see I pass a string as the id when I init @label = label @uniqueId = uniqueId end end ================================== class SearchWidgetComparisonComponent < SearchWidgetComponent def initialize(label, uniqueId) super(label, uniqueId) @dbFields = Array.new @showCalendar = false; end end in my index.rhtml, I have (please note I've added comments below only to this post, they are not in the source): <%= @sw.printcomps %> # prints "swCompareComp1", and "anoth" (my two unique ids) # should return a SearchWidgetComparisonComponent <% @obj = @sw.component("swCompareComp1") %> # call method stylize() on the object <%= @obj.stylize("sdbfPulldown", "scPulldown", "svfClass")%> ; however, the line that calls the stylize() method returns an error saying " NoMethodError in Region#index......You have a nil object when you didn't expect it! The error occured while evaluating nil.stylize" Thanks ahead for the help yo. I've been tearin my hair out for 5 hours now. -- Posted via http://www.ruby-forum.com/.