On Fri, Feb 4, 2011 at 8:04 PM, Jens F. [email protected]
wrote:
Is it not possible to call an array outside a defintion? Like this:
list = Array.new
^^^^^ lose this. not needed since you created list below
list = [“a”, “b”, “c”]
def do_stuff()
list.each_index do |i|
puts list[i]
end
end
do_stuff()
list would not be visible since we’re in oo env and encapsulation is
enforced.
If I define list inside do_stuff() the script works. My plan was to
build an array through several functions.
since you’re doing it procedurally, then why not pass list as a param.
eg,
def do_stuff(list)
list.each_index do |i|
puts list[i]
end
end
#=> nil
list = [“a”, “b”, “c”]
#=> [“a”, “b”, “c”]
do_stuff(list)
a
b
c
#=> [“a”, “b”, “c”]
scheme 2, you can make list a class variable,
eg,
def do_stuff
@@list.each_index do |i|
puts @@list[i]
end
end
#=> nil
@@list = [“a”, “b”, “c”]
#=> [“a”, “b”, “c”]
do_stuff
a
b
c
#=> [“a”, “b”, “c”]
best regards -botp