Does anyone know how can I override ruby''s Array delete method, and use my own instead? Cheers, Michal --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
michau wrote:> Does anyone know how can I override ruby''s Array delete method, and > use my own instead? > > Cheers, > > MichalSure 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... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On 7/20/07, michau <michalt48-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > Does anyone know how can I override ruby''s Array delete method, and > use my own instead? > > Cheers, > > Michal > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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"} On 7/20/07, michau <michalt48-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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 > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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"} > > MichalAlthough 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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---