From: Andrew Wagner Date: 2011-02-04T21:17:24+09:00 Subject: Re: Arrays and defs --001636c5984317353e049b73dacd Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi Jens, The problem here is that whenever you type "def", you start a new scope. An= d you can't directly access something (other than a global) in a different scope. Here's how I might recommend approaching this: class MyClass def initialize @list =3D ["a","b","c"] end def do_stuff list.each do |x| puts x end end end mc =3D MyClass.new mc.do_stuff Besides the technical changes, I've made a couple other simple modification= s to make your code more ruby-esque. These are stylistic, but worth learning: 1.) There's no point in putting Array.new in a variable before putting an actual list in there. You can just put the list in it from the beginning. (And, by the way, [] is equivalent to, and more idiomatic than, Array.new) 2.) Empty parens are not needed in method signatures (or, for the most part= , in any method signatures 3.) Unless you particularly need the index, you can use each to do somethin= g directly with each item in a list. Good luck! On Fri, Feb 4, 2011 at 7:04 AM, Jens Finn=C3=A4s wr= ote: > Hi, I've just started learning Ruby for screen scraping purposes and > I've now run in to a problem I can't get my head around. > > Is it not possible to call an array outside a defintion? Like this: > > list =3D Array.new > list =3D ["a", "b", "c"] > > def do_stuff() > list.each_index do |i| > puts list[i] > end > end > > do_stuff() > > If I define list inside do_stuff() the script works. My plan was to > build an array through several functions. > > -- > Posted via http://www.ruby-forum.com/. > > --001636c5984317353e049b73dacd--