# How do I modify a local variable through ruby\_block?

**URL:** <https://discourse.chef.io/t/how-do-i-modify-a-local-variable-through-ruby-block/2862>\
**Category:** Chef Infra (archive)\
**Created:** [June 22, 2012, 9:54pm UTC](https://discourse.chef.io/t/how-do-i-modify-a-local-variable-through-ruby-block/2862 "2012-06-22T21:54:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_S\_Kim](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@Daniel\_S\_Kim](https://discourse.chef.io/u/Daniel_S_Kim)\
**Post date:** [June 22, 2012, 9:54pm UTC](https://discourse.chef.io/t/how-do-i-modify-a-local-variable-through-ruby-block/2862/1 "2012-06-22T21:54:03Z")

</div>

Hello all,

I am having hard time with a variable modification. See the snippet below:  
subdir = ““  
ruby\_block “read target archive directory name” do  
block do  
Dir.foreach(”/snapshot/#{dbSnapshotId}”) do |f|  
unless(f.to\_s.eql? “.”)  
unless (f.to\_s.eql? “…”)  
Chef::Log.info("Timestamp: " + f.to\_s)  
subdir = f.to\_s  
node[:RestoreSubdir] = subdir  
node.save  
end  
end  
end  
end  
action :create  
end

After the ruby\_block, subdir is still an empty string. However, Chef::Log shows  
a value otherwise. I also tried these other method in order to get this String  
outside the ruby\_block scope:

1. script resource =\> variable modification undone once out of scope
2. ruby\_block resource =\> variable modification undone once out of scope
3. separate recipe with ruby code =\> ruby code still runs 1st before mounting  
the drive
4. separate recipe with ruby\_block/script =\> still cannot see the variable  
modification once out of scope
5. node.save within ruby\_block =\> save doesn’t occur until chef-client is done  
running all the resources
6. databag.save within ruby\_block =\> save doesn’t occur until chef-client is  
done running all the resources

Any suggestion?

Regard,

Daniel K.
