4 Replies - 2355 Views - Last Post: 31 May 2012 - 06:05 AM Rate Topic: -----

#1 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

I am having problems with generating arrays for some reason adding nil

Posted 29 May 2012 - 02:08 PM

Hey I trying to create this array.
But I get a HOLE lot of nils, can anyone tell me what the f... I am doing wrong?

   @largeLetters = Array.new(26)
   @largeLetters.push("A", "B", "C", "D",
                         "E", "F", "G", "H",
                         "I", "J", "K", "L",
                         "M", "N", "O", "P",
                         "Q", "R", "S", "T",
                         "U", "V", "W", "X",
                         "Y", "Z")

  puts "Large Array"
       @largeLetters.each{
         |item| print item, "\n"
       }



but my output:

56347d1b:EncryptText tools$ ruby App.rb 
Large Array
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
nil
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z





- Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: I am having problems with generating arrays for some reason adding nil

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: I am having problems with generating arrays for some reason adding nil

Posted 29 May 2012 - 02:13 PM

So you create an array with size 26, then push 26 letters onto the end. Your array's total size is probably 52. In Ruby, you don't need to initialize the space ahead of time - just create the array without an initial size, because that parameter assigns those 26 positions the value of the second argument (which you didn't pass, so it defaults to nil) or the block (which you didn't pass, so it evaluates to nil).

for reference:
http://www.ruby-doc....ml#method-c-new

edit:

I'm suggesting doing this:
largeArray = []
Or:
largeArray = Array.new
instead of sending 26.

Alternatively:
largeArray = ('A'..'Z').to_a to do it all at once.

This post has been edited by xclite: 29 May 2012 - 02:15 PM

Was This Post Helpful? 2
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: I am having problems with generating arrays for some reason adding nil

Posted 29 May 2012 - 02:35 PM

View Postxclite, on 29 May 2012 - 11:13 PM, said:

I'm suggesting doing this:
largeArray = []
Or:
largeArray = Array.new
instead of sending 26.


There's very little point in creating an empty array and then calling push on it if he spells out every single element anyway. He could just as well do:

@large_array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]



Quote

Alternatively:
largeArray = ('A'..'Z').to_a to do it all at once.


That's way better of course.
Was This Post Helpful? 0
  • +
  • -

#4 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Re: I am having problems with generating arrays for some reason adding nil

Posted 29 May 2012 - 03:01 PM

Thanks a lot
Was This Post Helpful? 0
  • +
  • -

#5 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: I am having problems with generating arrays for some reason adding nil

Posted 31 May 2012 - 06:05 AM

...or you could use something like %w[a b c d e f g ... ] but given that it's a linear progression in the alphabet you can just use a loop to generate it all.

EDIT - Someone beat me to the loop, my bad.

This post has been edited by Lemur: 31 May 2012 - 06:05 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1