Hi Jeff,
I did need the find_all. What's interesting is I find that I can do the
following:
(1) Have my selection method in the StepList class look like this:
def selection
@guides.find_all { |guide| guide }
end
(2) Have my 'each' method in the Step class look like this:
def each
end
Actually you can't. You are just not calling this each method.
>> def each; end
=> nil
>> each
=> nil
You see, it will just return nil. So it must be something different you are calling.
(3) Cycle through the elements like this:
$stepList.selection.each { |thisStep|
puts thisStep
}
You call each on the returned object of "selection". And that is an array ... So you are calling Array#each.
Btw. It is perfectly legal to use camelCase for variable- and method names, like you would do in Java, and a lot of people do it, but most of the time I see people using "camel_case". thisStep -> this_step ...
It seems that you don't need your own "each" after all.
If I run that above code (from (3)), I get this:
Step Guide: 202G_OrdAdd, 203G_OrdUpdate, first after
202G_OrdAdd|203G_OrdUpdateFirst
Step Guide: 202G_OrdAdd, 203G_OrdUpdate, last|203G_OrdUpdateLast
Step Guide: 203G_OrdUpdate, 203G_OrdUpdate, last|203G_OrdUpdateLast
Here the last part (after the second comma) is actually the filter part I
want. With this, however, I can at least parse the string and get to that.
Uuh. Don't do that.
I looked at your code again, but didn't find the part where you add the elements.
I guess you call this method:
def append(thisGuide)
@guides.push(thisGuide)
self
end
But what do you add? I would assume an instance of Step?
I should note that to get the above output, it was necessary to have a to_s
method in the Step class. Otherwise, I would get this when the
'selection.each' was run:
#<Step:0x2a9b15c>
#<Step:0x2a9a338>
#<Step:0x2a995dc>
I think what was (perhaps still is) throwing me off is that I have an object
(Step) being stored within another object (StepList).
Yeah, maybe. In this particular case I would like to see the to_s method what the information after the 2nd comma is.
I take it that you want to output a particular attribute of Step, but you ask "puts" to output the whole object:
$stepList.selection.each { |thisStep|
puts thisStep
}
So what you could do is this: "puts thisStep.attributename"... As you've put attr_reader :point1, :point2, :filter in your Step class you will be able to access these three attributes this way.
Bonus Answer (if you're choking on the stuff above, ignore this):
I assume that this "puts" thing is a step to the actual goal. You probably just want all the attribute values in an Array? To do that you could use the following code:
$steps.selection.collect {|step| step.point1}
And the code will return an array of point1s. Collect will create a new array with all of the return values of the block. And you get this feature for free as Array implements the each method. That enables Array to include the Enumerable module. This module only needs this one method, each, and will provide other methods for you that each use each (no pun intended). Beautiful? Yes! "find" and "find_all" from above work the same way. "find" for example call each as long as the block will return "true". Have a look at http://corelib.rubyonrails.org/classes/Enumerable.html\.
Cheers,
Mariano
···
On Oct 3, 2006, at 8:20 PM, Jeff Nyman wrote: