6 Replies - 1457 Views - Last Post: 12 June 2017 - 11:53 PM Rate Topic: -----

#1 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Looping through my array and putting the words in columns

Posted 09 June 2017 - 11:28 PM

Hello
I have a words_array saved to an instance variable and I am trying to loop through it and take out all new lines and put it in nice columns, I have 45 words and they are in the array like this...

["APPLE", "APRICOT", "AVOCADO", "BREADFRUIT", "BANANA", "BLACKBERRY", "BLACKCURRANT",

I tried gsub, and .join but it either doesn't work or I am doing it really wrong, here is the start of the loops so far but I'm clueless on how to make it in nice even columns that line up side by side..

def print_words
    @words_array.each do |word|
      puts word
    end



It's not much but I just deleted the last gsub method I tried on it.

Thanks in advance

This post has been edited by jstanley6: 09 June 2017 - 11:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Looping through my array and putting the words in columns

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: Looping through my array and putting the words in columns

Posted 10 June 2017 - 12:47 AM

I cannot really follow your description, but if you want the output to appear on the same line then your could use print rather than puts.

You could also use join, please show your attempt at this to receive help.
Was This Post Helpful? 0
  • +
  • -

#3 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Looping through my array and putting the words in columns

Posted 10 June 2017 - 09:58 AM

its not on the same line though, its more like on separate lines but only a certain amount of words. Since 5 is even with 45, it might place 5 words in 9 columns or 9 words in 5 columns, the way a word search puts the list of words to find. I have been researching it and still have no clue what would work. I know that gsub and join might not work.split seems like it won't work either. I'm just confused. I have the program finished already except this part. I have the algorithm for the word search puzzle done, I have the answer key printed out to a pdf and then i have the word search with random letters generated in place of the '.''s on a new pdf page.
Was This Post Helpful? 0
  • +
  • -

#4 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Looping through my array and putting the words in columns

Posted 10 June 2017 - 10:16 AM

Also another thing I don't get, is it prints out to the console like this..
APPLE
APRICOT
AVOCADO
BREADFRUIT
BANANA
BLACKBERRY

but prints to the pdf like this..
["APPLE", "APRICOT", "AVOCADO", "BREADFRUIT",

I'm calling the method in my pdf like this

pdf.start_new_page
puz.print_words
pdf.text "#{puz.print_words}"

not sure why it still wants to format it in the pdf like an array but prints to the console with new lines
Was This Post Helpful? 0
  • +
  • -

#5 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Looping through my array and putting the words in columns

Posted 10 June 2017 - 08:27 PM

nevermind i got it
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4081
  • View blog
  • Posts: 18,152
  • Joined: 24-September 08

Re: Looping through my array and putting the words in columns

Posted 12 June 2017 - 11:22 AM

Could you please post your solution so others can see?
Was This Post Helpful? 0
  • +
  • -

#7 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Looping through my array and putting the words in columns

Posted 12 June 2017 - 11:52 PM

  def print_words
    ret_str = ''
    @words_array.each do |word|
      ret_str += "#{word.ljust(20)}"
    end
    return ret_str
  end


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1