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