Overriding Array


#1

Does anyone know how can I override ruby’s Array delete method, and
use my own instead?

Cheers,

Michal


#2

Hi –

On Fri, 20 Jul 2007, michau wrote:

Does anyone know how can I override ruby’s Array delete method, and
use my own instead?

class Array
def delete(obj)

end
end

However, it doesn’t sound like a good idea, as you’ll be clobbering a
core method. You’d probably be better off doing something like:

module MyArray
def delete(obj)

end
end

a = [].extend(MyArray)

David


#3

michau wrote:

Does anyone know how can I override ruby’s Array delete method, and
use my own instead?

Cheers,

Michal

Sure you want to do that ? :slight_smile:

I think it’s as easy as:
Class Array
def delete(object)
your_code
end
end

I might be forgetting something…


#4

put the following into lib/helpers.rb (or some other file in /lib) and
then put: require ‘lib/helpers.rb’ into your environment.rb. Put the
following into your helpers.rb file:

class Array
def delete(*arg, &block)
do my stuff
end
end

Adam


#5

Sure you want to do that ? :slight_smile:

I’m not sure. The rails method generated by has_many declaration
returns an Array though, right?
I want to be able to delete from that array, effectively destroying
rows in the database through a call like
@returned_array_of_my_objects.delete {|object| object.name == “name”}

Michal


#6

there’s an easier way of doing that…

article.rb
has_many :users

users = @some_article.users

users.each {|user| user.destroy if user.name == “delete me”}


#7

On Jul 20, 2007, at 3:27 PM, michau wrote:

Sure you want to do that ? :slight_smile:

I’m not sure. The rails method generated by has_many declaration
returns an Array though, right?
I want to be able to delete from that array, effectively destroying
rows in the database through a call like
@returned_array_of_my_objects.delete {|object| object.name == “name”}

Michal

Although it masquerades as an Array, the result of a has_many
association is actually an association proxy that still mixes in a
large amount of ActiveRecord goodness.

From a handy script/console session:

Here’s a has_many association: (see the @macro instance variable)

User.reflect_on_association(:favorites)
=> #<ActiveRecord::Reflection::AssociationReflection:0x3312290
@options={:dependent=>:destroy, :include=>:product},
@class_name=“Favorite”, @active_record=User, @klass=Favorite,
@macro=:has_many, @primary_key_name=“user_id”,
@through_reflection=false, @name=:favorites>

The favorites say they are what?

User.find(1).favorites.class
=> Array

Oh, really?

User.find(1).favorites.ancestors
=> [Favorite, ActiveRecord::Base, ActiveRecord::AttributeMethods,
ActiveRecord::XmlSerialization, ActiveRecord::Calculations,
ActiveRecord::Acts::NestedSet, ActiveRecord::Acts::List,
ActiveRecord::Acts::Tree, ActiveRecord::Reflection,
ActiveRecord::Transactions, ActiveRecord::Aggregations,
ActiveRecord::Associations, ActiveRecord::Timestamp,
ActiveRecord::Observing, ActiveRecord::Callbacks,
ActiveRecord::Locking::Pessimistic,
ActiveRecord::Locking::Optimistic, ActiveRecord::Validations,
Reloadable::Deprecated, Object, PP::ObjectMixin, Base64::Deprecated,
Base64, Kernel]

…but an Array says:

Array.ancestors
=> [Array, ActiveSupport::CoreExtensions::Array::Grouping,
ActiveSupport::CoreExtensions::Array::Conversions, Enumerable,
Object, PP::ObjectMixin, Base64::Deprecated, Base64, Kernel]

And in a plain irb session:

irb(main):001:0> Array.ancestors
=> [Array, Enumerable, Object, Kernel]

I’m not sure whether the delete with block syntax you gave will work
on the “Array” (but I think that it does, try it in a safe
environment), but there is a delete method of the association that
takes a list of associated objects to be deleted and if they are in
a :dependent => :destroy relationship, the database record will be
removed, too.

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid