Arrays and defs

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[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.

Hi Jens,

The problem here is that whenever you type “def”, you start a new scope.
And
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 = [“a”,“b”,“c”]
end

def do_stuff
list.each do |x|
puts x
end
end
end

mc = MyClass.new
mc.do_stuff

Besides the technical changes, I’ve made a couple other simple
modifications
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
something
directly with each item in a list.

Good luck!

My recommends are

  1. overwrite Array

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

end

  1. include module

module MyModule
def do_stuff()
self.each_index do |i|
puts self[i]
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

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.

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

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[i]
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs