# \[newbie\] make local var visible

**URL:** <https://rubytalk.org/t/newbie-make-local-var-visible/19808>\
**Category:** ruby-talk\
**Created:** [29 July 2005 11:55 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19808 "2005-07-29T11:55:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [29 July 2005 11:55 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19808/1 "2005-07-29T11:55:11Z")

</div>

#\> Hi Brian/Cristi/Wannes/Kroeger/Kero, and others,  
#\>  
#\> Yes. I've done what you have all suggested. Thank you.  
#\> But I was hoping for something "cleaner". Sorry.  
#\>  
#\> Can we request RCR for this?

> **···**
>
> Stefan Lang [[mailto:langstefan@gmx.at](mailto:langstefan@gmx.at)] wrote:
> 
> #  
> #IMHO not necessary. Ruby already has what you want. You  
> #want a closure.  
> #  
> #foo=["a","b","c"]  
> #  
> #mfoo = lambda do  
> # foo.each do |f|  
> # p f  
> # end  
> #end  
> #  
> #mfoo.call  
> #
> 
> Yes, Stefan. Thank you. Lambda and proc have been suggested.  
> But i still want something cleaner.
> 
> Can we make something like proc but do it inside out? We do not adjust the  
> methods but the vars, that is.
> 
> I am just trying to bridge the gap of bash/perl/sh and ruby.
> 
> I do not think presenting lambda is welcoming to my non-programmer peers.  
> (Note, as stated in my op, i am converting sh/perl/ scripts..)
> 
> thanks and kind regards -botp
> 
> #--  
> #Stefan  
> #

---

<div class="post-metadata">

**Author:** ![Jim\_Weirich1](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jim\_Weirich1](https://rubytalk.org/u/Jim_Weirich1)\
**Post date:** [29 July 2005 12:13 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19808/2 "2005-07-29T12:13:04Z")

</div>

Then your choices are to

A) make it global (ie. $foo), or  
B) make it a constant (ie. Foo).

> **···**
>
> On Friday 29 July 2005 07:55 am, "Peña, Botp" wrote:
> 
> > Can we make something like proc but do it inside out? We do not adjust the  
> > methods but the vars, that is.
> 
> --  
> -- Jim Weirich jim@weirichhouse.org [http://onestepback.org](http://onestepback.org)  
> -----------------------------------------------------------------  
> "Beware of bugs in the above code; I have only proved it correct,  
> not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

---

<div class="post-metadata">

**Author:** ![Stefan\_Lang](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Stefan\_Lang](https://rubytalk.org/u/Stefan_Lang)\
**Post date:** [29 July 2005 13:01 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19808/3 "2005-07-29T13:01:52Z")

</div>

> #IMHO not necessary. Ruby already has what you want. You  
> #want a closure.  
> #  
> #foo=["a","b","c"]  
> #  
> #mfoo = lambda do  
> # foo.each do |f|  
> # p f  
> # end  
> #end  
> #  
> #mfoo.call  
> #
> 
> Yes, Stefan. Thank you. Lambda and proc have been suggested.  
> But i still want something cleaner.

I hope you can understand what I mean, English is not my native  
language:

It seems that you want to access some state from within the method.  
In Perl, this state would be the variables in the enclosing  
scope, since every sub in Perl is essentielly a closure (AFAIK).

In Ruby, the state which a method has access to is self, with  
its instance variables. So for your purpose, use instance variables.  
And to make it even cleaner, try to wrap it up in a class.

> Can we make something like proc but do it inside out? We do not  
> adjust the methods but the vars, that is.
> 
> I am just trying to bridge the gap of bash/perl/sh and ruby.

You can fake Perl's subs with Ruby's lambdas, but that would be  
far from idiomatic.

Let's translate a little Perl script to Ruby:

% cat count\_files.pl  
my @dirs = @ARGV;  
@dirs = (".") unless @dirs;  
my $file\_count = 0;

sub count\_files\_in\_dir {  
&nbsp;&nbsp;&nbsp;&nbsp;my $dir = shift;  
&nbsp;&nbsp;&nbsp;&nbsp;opendir(DH, $dir);  
&nbsp;&nbsp;&nbsp;&nbsp;my @files = grep { -f "$dir/$\_" } readdir(DH);  
&nbsp;&nbsp;&nbsp;&nbsp;closedir(DH);  
&nbsp;&nbsp;&nbsp;&nbsp;$file\_count += scalar(@files);  
}

for my $dir (@dirs) { count\_files\_in\_dir($dir); }

print "Number of files: $file\_count\n";

> **···**
>
> On Friday 29 July 2005 13:55, "Peña, Botp" wrote:  
> ###########################################################  
> % cat count\_files.rb  
> class FileCounter  
> &nbsp;&nbsp;&nbsp;&nbsp;def initialize(dirs)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@dirs = dirs  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;def file\_count  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @file\_count if defined? @file\_count  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@file\_count = 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@dirs.each { |d| count\_files\_in\_dir d }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@file\_count  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;def show  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Number of files: #{file\_count}"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;private  
> &nbsp;&nbsp;&nbsp;&nbsp;def count\_files\_in\_dir(d)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.foreach(d) { |fn| @file\_count += 1 if test ?f,  
> "#{d}/#{fn}" }  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> FileCounter.new(ARGV.empty? ? ["."] : ARGV).show  
> ###########################################################  
> Both print the number of files in the current directory  
> or in the directories given on the commandline.
> 
> What's the difference: The Ruby script can have any number  
> of FileCounter objects, the program state is hold (in the  
> above script) by one FileCounter object.
> 
> The program state in the Perl script is hold by the variables  
> @dirs and $file\_count, there can't exist a second $file\_count  
> or @dirs.
> 
> Kind regards,  
> &nbsp;&nbsp;Stefan
