From: dblack@... Date: 2006-12-02T06:55:39+09:00 Subject: Re: Simple iteration in a function problem Hi -- On Sat, 2 Dec 2006, Blake Miller wrote: > 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. I wish I could help. I can't spot anything that would cause @obj to be nil, and when I try a standalone version of your code (essentially what you've got minus the ERB markup), it runs fine. Is it possible you've got symbols in one place (maybe when you create the components) and strings in another? 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