# My goal for the following function is to make an array of objects, and then # make a new object that containts this list of objects. And to be able to # see that it works a function that goes trough each element of objects. # Does not work :-( Any ideas ? # I have a an hash with objects and a class with these objects loaded_plugins = {} loaded_plugins[0] = Class.new loaded_plugins[1] = Class.new loaded_plugins[2] = Class.new class Scan @plugins def initialize(block) { @plugins = block } def rubyfunc each_item do |item| puts "Rubyforum" end end def each_item(&block) { block.call(@plugins) } end # And I send this array(loaded_plugins) to a class: scan = Scan.new(loaded_plugins) scan.rubyfunc => "Rubyforum" (Once, not three times) -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jan-24 15:42 UTC
Re: Troubles having arrays of objects inside a class
On 24 Jan 2008, at 15:33, Andreas Secret wrote:> > # My goal for the following function is to make an array of objects, > and > then > # make a new object that containts this list of objects. And to be > able > to > # see that it works a function that goes trough each element of > objects. > # Does not work :-( Any ideas ? > > > # I have a an hash with objects and a class with these objects > loaded_plugins = {} > loaded_plugins[0] = Class.new > loaded_plugins[1] = Class.new > loaded_plugins[2] = Class.new > > class Scan > @plugins > > def initialize(block) { @plugins = block } > > def rubyfunc > each_item do |item| > puts "Rubyforum" > end > end > def each_item(&block) { block.call(@plugins) } > end >> # And I send this array(loaded_plugins) to a class: > > scan = Scan.new(loaded_plugins) > scan.rubyfunc > > > => "Rubyforum" (Once, not three times)That''s not surprising - you''re only calling the block once (and your passing in the hash). You probably want each_item to read @plugins.each {|plugin| block.call(plugin)} Fred> > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 24 Jan 2008, at 15:33, Andreas Secret wrote: > >> >> def initialize(block) { @plugins = block } >> >> def rubyfunc >> each_item do |item| >> puts "Rubyforum" >> end >> end >> def each_item(&block) { block.call(@plugins) } >> end >> > >> # And I send this array(loaded_plugins) to a class: >> >> scan = Scan.new(loaded_plugins) >> scan.rubyfunc >> >> >> => "Rubyforum" (Once, not three times) > > That''s not surprising - you''re only calling the block once (and your > passing in the hash). > You probably want each_item to read > @plugins.each {|plugin| block.call(plugin)} > > FredGreat, good point! What if I want to access that certain objects functions ? (i.e. not having a hash, but converting it back to a real class) => Changing the inside function from above: ... def rubyfunc each_item do |item| item.get_another_function(self) <- Wouldn''t work end end -- 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 -~----------~----~----~----~------~----~------~--~---