# Troubles having arrays of objects inside a class

**URL:** <https://www.ruby-forum.com/t/troubles-having-arrays-of-objects-inside-a-class/127048>\
**Category:** Rails\
**Created:** [January 24, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/troubles-having-arrays-of-objects-inside-a-class/127048 "2008-01-24T16:33:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [January 24, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/troubles-having-arrays-of-objects-inside-a-class/127048/1 "2008-01-24T16:33:50Z")

</div>

# 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)

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [January 24, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/troubles-having-arrays-of-objects-inside-a-class/127048/2 "2008-01-24T16:43:05Z")

</div>

On 24 Jan 2008, at 15:33, Andreas S. 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)}

Fred

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [January 24, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/troubles-having-arrays-of-objects-inside-a-class/127048/3 "2008-01-24T16:59:35Z")

</div>

Frederick C. wrote:

> On 24 Jan 2008, at 15:33, Andreas S. 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)}
> 
> Fred

Great, 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
