i wonder, a coding challenge of sorts, what would it talk to create an
Array_Of(AClass) constructor? such that an array would be created that can
only contain objects of the class specified.
Pretty simple, I would think. Find all the methods that can add or change
elements in the array, and add type checks to it.
Tim Bates
···
On Mon, 13 Jan 2003 05:07 pm, Tom Sawyer wrote:
i wonder, a coding challenge of sorts, what would it talk to create an
Array_Of(AClass) constructor? such that an array would be created that can
only contain objects of the class specified.
Sorry, I give up. I can’t see how to do it elegantly. I guess it’s
just a poor fit for Ruby.
My approach was like this:
def ArrayOf(klass, *args)
the_array = Array.new(*args)
class << the_array
···
On Monday, January 13, 2003, 5:37:21 PM, Tom wrote:
i wonder, a coding challenge of sorts, what would it talk to create an
Array_Of(AClass) constructor? such that an array would be created that can
only contain objects of the class specified.
#
# Redefine ... here
#
end
return the_array
end
Where “…” is
+(ary)
=(range, ary)
<<(obj)
=(idx, obj)
=(start, length, obj)
push(obj)
unshift(obj)
The methods are grouped like that because you need to typecheck the
last argument always. In the first two cases that argument will be an
array (and you need to check the type of all the elements). For the
rest you need to typecheck a single object.
= is a special case. The last argument can be an object OR an
array.
I think the approach of using a singleton class is good, because you
can access the underlying Array method with “super”. My first though
was delegation, or even a blank class with just #method_missing.
However, in these cases, the resulting object wouldn’t pass the “is_a?
Array” test.
I want to highlight this: from a singleton class, you can use “super”
like in normal inheritance. Keep this in mind if you are redefining
methods anytime. I find the pattern
alias_method :old_xxx, :xxx
def xxx
…
old_xxx
…
end
rather smelly. Much better is
class << obj
def xxx
…
super
…
end
end
I just learned that through this exercise, so I’m glad some good came
of it.
i wonder, a coding challenge of sorts, what would it talk to create an
Array_Of(AClass) constructor? such that an array would be created that can
only contain objects of the class specified.
Oh, so you want type checking now?
Pretty simple, I would think. Find all the methods that can add or change
elements in the array, and add type checks to it.
–
Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)
Sorry, I give up. I can’t see how to do it elegantly. I guess it’s
just a poor fit for Ruby.
yes, i have found it tricky too. you made a good start of it though. i’ll play
with it later and see what i can come up with.
I think the approach of using a singleton class is good, because you
can access the underlying Array method with “super”. My first though
was delegation, or even a blank class with just #method_missing.
However, in these cases, the resulting object wouldn’t pass the “is_a?
Array” test.
I want to highlight this: from a singleton class, you can use “super”
like in normal inheritance. Keep this in mind if you are redefining
methods anytime. I find the pattern
…
I just learned that through this exercise, so I’m glad some good came
of it.
ah now that is interesting! i’ll have to keep this in mind from hence forth.
thanks!
···
On Monday 13 January 2003 12:38 am, Gavin Sinclair wrote:
I want to highlight this: from a singleton class, you can use “super”
like in normal inheritance. Keep this in mind if you are redefining
methods anytime. I find the pattern
alias_method :old_xxx, :xxx
def xxx
…
old_xxx
…
end
rather smelly. Much better is
class << obj
def xxx
…
super
…
end
end
I just learned that through this exercise, so I’m glad some good came
of it.
Technique #2 is not a drop-in replacement for technique #1, though.
For example, if you want to “listen in” on what a method is doing,
you might do:
class String
alias :oldsplit :split
def split(*a)
puts “args are #{a.join(', ')}”
oldsplit(*a)
end
end
I don’t think there’s an equivalent way to do this by modifying the
object’s singleton class.