Brian Candler
4/11/2007 7:34:00 AM
On Wed, Apr 11, 2007 at 03:23:52PM +0900, Phillip Gawlowski wrote:
> ClothRed#to_textile used Array#collect! to iterate over the string that
> was to be transformed, which let the first test complete successfully as
> it was only testing for *one* condition. As soon as I change #collect!
> to #each, the code behaved as the test expected. Although why this
> happens, I'm not sure (I guess the receiver of the block is the "count"
> for the block, and not the elements in the Array).
I don't understand what you've written above - you might want to paste an
example. For both Array#each and Array#collect!, the values passed to the
block are the elements of the array. The difference is in what happens to
the value returned by the block; #each ignores it, #collect! replaces the
original array element with it.
As a separate point though: if to_textile changes the state of your ClothRed
object, you might want to consider calling it something else.
Normally, a to_foo method returns an object of type foo, and does not change
the state of the receiver. Compare Object#to_yaml, Method#to_proc,
Object#to_enum and so on.
Personally I would be surprised to find a Ruby library with a "to_textile"
method, where the main purpose of the function was to change the state of
the receiver, and _not_ to return a textile object.
Regards,
Brian.