Arrays and defs

J

Jens Finnäs

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 = Array.new
list = ["a", "b", "c"]

def do_stuff()
list.each_index do |i|
puts list
end
end

do_stuff()

If I define list inside do_stuff() the script works. My plan was to
build an array through several functions.
 
A

Andrew Wagner

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!
 
J

Jens Finnäs

Cheers. Thanks for all the advice. I have to admit that I'm not very
structured in my learning. It's been mostly trial and error so far.
 
M

masa

My recommends are

1. overwrite Array

class Array
def do_stuff()
self.each_index do |i|
puts self
end
end

end

2. include module

module MyModule
def do_stuff()
self.each_index do |i|
puts self
end
end
end

class Array
include MyModule
end

Then

list = Array.new
list = ["a", "b", "c"]
list.do_stuff

Or
you can include the module to any class inherits Array class as you like

like here

class MyClass < Array
include MyModule
end

Best
Masa
 
B

botp

Is it not possible to call an array outside a defintion? Like this:
list =3D Array.new
^^^^^ lose this. not needed since you created list below
list =3D ["a", "b", "c"]

def do_stuff()
=A0list.each_index do |i|
=A0 =A0puts list
=A0end
end
do_stuff()


list would not be visible since we're in oo env and encapsulation is enforc=
ed.
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
end
end
#=3D> nil

list =3D ["a", "b", "c"]
#=3D> ["a", "b", "c"]
do_stuff(list)
a
b
c
#=3D> ["a", "b", "c"]


scheme 2, you can make list a class variable,

eg,

def do_stuff
@@list.each_index do |i|
puts @@list
end
end
#=3D> nil

@@list =3D ["a", "b", "c"]
#=3D> ["a", "b", "c"]
do_stuff
a
b
c
#=3D> ["a", "b", "c"]


best regards -botp
 
B

Brian Candler

botp wrote in post #979636:
since you're doing it procedurally, then why not pass list as a param.

Good idea here.
scheme 2, you can make list a class variable,

eg,

def do_stuff
@@list.each_index do |i|
puts @@list
end
end
#=> nil


I'd recommend avoiding class variables (@@) as something of a ruby
misfeature - especially outside of an explicit class. Even a global
variable ($list) would be better here.

However, in a typical application, the data and the methods would be
part of the same object.

class Thing
def initialize(list)
@list = list
end
def do_stuff
@list.each do |elem|
puts elem
end
end
end

thing = Thing.new(["a","b","c"])
thing.do_stuff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top