[newbie] make local var visible

#>> I think you have 2 options here (maybe more, but these are
#the ones I
#>> can come up with as a newbie myself):

···

#
#four and counting...
#
#>> 1) pass foo as an argument.
#>> def mfoo(foo) ... end
#>> mfoo(foo)
#>> 2) make foo an instance-variable
#>> @foo = [..]
#>> def mfoo
#>> @foo.each...
#>> end
#>
#> or make it global (sigh): $foo
#
#or make a proc from the method

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?

I like the private/public clause for methods. I hope we can provide
something like it for vars though; that is, make local vars "public" or
"private"

like,

#test.rb
#sample script---------------->8

private_var foo # <--declare something like this or whatever
                  # foo will be visible up to end of test.rb only
                  # as if foo now becomes @foo, or $foo, or what

foo=["a","b","c"]

def mfoo
    foo.each do |f|
        p f
    end
end

# go-go-go!
mfoo
#end sample script------------>8

Will this break ruby design?
If yes, please forget i asked.

Thanks and kind regards -botp

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

···

On Friday 29 July 2005 13:09, "Peña, Botp" wrote:

#>> I think you have 2 options here (maybe more, but these are
#the ones I
#>> can come up with as a newbie myself):
#
#four and counting...
#
#>> 1) pass foo as an argument.
#>> def mfoo(foo) ... end
#>> mfoo(foo)
#>> 2) make foo an instance-variable
#>> @foo = [..]
#>> def mfoo
#>> @foo.each...
#>> end
#>
#> or make it global (sigh): $foo
#
#or make a proc from the method

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

Hi,

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?

How about using class variables?

  module FooTest
    @@foo=["a","b","c"]

    def mfoo
      @@foo.each do |f|
          p f
      end
    end
  end

"@@foo" can be accessed from anywhere in the FooTest body.

              matz.

···

In message "Re: [newbie] make local var visible" on Fri, 29 Jul 2005 20:09:51 +0900, "Peña, Botp" <botp@delmonte-phil.com> writes:

maybe:

   harp:~ > cat a.rb
   require 'traits'

   trait 'foo' => %w( a b c )

   def mfoo
     foo.each{|f| p f}
   end

   mfoo

   harp:~ > ruby a.rb
   "a"
   "b"
   "c"

or you can write this

   require 'traits'

   trait 'foo'

   foo 42

   def mfoo
     foo.each{|f| p f}
   end

   mfoo

which is kinda close to what you want.

hth.

-a

···

On Fri, 29 Jul 2005, [iso-8859-1] "Peña, Botp" wrote:

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?

I like the private/public clause for methods. I hope we can provide
something like it for vars though; that is, make local vars "public" or
"private"

like,

#test.rb
#sample script---------------->8

private_var foo # <--declare something like this or whatever
                 # foo will be visible up to end of test.rb only
                 # as if foo now becomes @foo, or $foo, or what

foo=["a","b","c"]

def mfoo
   foo.each do |f|
       p f
   end
end

# go-go-go!
mfoo
#end sample script------------>8

Will this break ruby design?
If yes, please forget i asked.

Thanks and kind regards -botp

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
My religion is very simple. My religion is kindness.
--Tenzin Gyatso

===============================================================================