Forum: Ruby on Rails Overriding Array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
59ee7c346d6578714bd6f42a15c8b9f7?d=identicon&s=25 michau (Guest)
on 2007-07-20 18:34
(Received via mailing list)
Does anyone know how can I override ruby's Array  delete method, and
use my own instead?

Cheers,

Michal
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-07-20 18:45
(Received via mailing list)
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

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)
4765b690d8d04b089b0a7b0ee7768ebc?d=identicon&s=25 Stijn Pint (spint)
on 2007-07-20 18:45
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 ? :-)

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

I might be forgetting something...
581905e940c1e30190108a0c5c0b915b?d=identicon&s=25 Adam Cohen (Guest)
on 2007-07-20 18:52
(Received via mailing list)
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
59ee7c346d6578714bd6f42a15c8b9f7?d=identicon&s=25 michau (Guest)
on 2007-07-20 21:28
(Received via mailing list)
>
> Sure you want to do that ? :-)
>


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
581905e940c1e30190108a0c5c0b915b?d=identicon&s=25 Adam Cohen (Guest)
on 2007-07-20 21:51
(Received via mailing list)
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"}
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-07-20 22:11
(Received via mailing list)
On Jul 20, 2007, at 3:27 PM, michau wrote:
>>
>> Sure you want to do that ? :-)
>>
> 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.