My concern is that this violates ducktyping. What are you going to do
different with this knowledge?
On 8/29/06, Tom Werner <[EMAIL PROTECTED]> wrote:
Sounds like a good chance to pull that functionality out into a function
for reuse:
class Array
def all_elements_are?(klass)
self.inject(true) { |same, n| same && n.instance_of?(klass) }
end
end
a = ['foo', 'bar', 'baz']
a.all_elements_are?(String) #=> true
b = ['foo', 5, 'baz']
b.all_elements_are?(String) #=> false
Tom
Chris Abad wrote:
> anyone know of a better way to make sure all items in an array are
> instances of the same class? specifically i want to make sure they're
> all instances of the same AR class.
>
> This just doesn't seem the best way:
>
> def method
> object_class = array.first.class
> array.each do | record |
> raise ArgumentError unless record.class == object_class
> end
> end
> _______________________________________________
> Sdruby mailing list
> [email protected]
> http://lists.sdruby.com/mailman/listinfo/sdruby
>
--
Tom Werner
Helmets to Hardhats
Software Developer
[EMAIL PROTECTED]
www.helmetstohardhats.org
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby