On Fri, Jan 09, 2009 at 12:16:10PM -0600, Luke Kanies wrote:
> It does create a local copy -- but it only creates the local copy
> after $6 has been set to a single value (the last one). Remember that
> the setcode block is lazy-evaluated; it's evaluated when the fact
> value is asked for, rather than when the fact is defined.
Okay. Because $6 is a global variable there's only one value associated with
it, and it is that value that is referenced when the setcode block is called.
In the case of the `local' variable below, the block/closure "remembers" its
instance value from the time of the definition but in the case of the
`$global' variable there is only one instance, whose value at the end is
common to all closures.
The following code demonstrates the difference:
lizzy:/tmp% cat x
class Foo
def setcode(&block)
@block = block
end
def value
@block.call
end
end
a = []
3.times do |i|
a << f = Foo.new
local, $global = i, i
f.setcode do
[local, $global]
end
end
a.each do |elem|
p elem.value
end
lizzy:/tmp% ruby x
[0, 2]
[1, 2]
[2, 2]
lizzy:/tmp%
Hope I got that right. Thanks again, Luke.
--
Jos Backus
jos at catnook.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en
-~----------~----~----~----~------~----~------~--~---