On Sun, Aug 25, 2013 at 5:59 PM, Ben G. <lists / ruby-forum.com> wrote:

> So I am trying to generate a list of my numbers in a rage and export
> that to a text file, wherein each number is written on it's own line. I
> have no problem generating the array


But why generate an Array first?  You don't need that.


> and writing it to file, I just
> can't figure out how to make each value appear on it's own line.
>
> Ex. I want this:
> 1
> 2
> 3
> 4
>
> Ex. My output is this
> 1, 2, 3, 4
>
> this is the section of my code that I am having problems with
>
> while start_number < end_number do
>   numberlist.push(start_number)
>   start_number = start_number +1
>   end
>
> File.open(city_name, 'w' ) do |w|
>   w.write(numberlist)
> end
>

The most simple solution if numberlist is an Array is probably this:

File.open(city_name, 'w') do |w|
  w.puts numberlist
end


> I have been searching online for a day and a half and I keep coming
> across json and yaml. My understanding is that these gems provide an
> intermediary standardized format that is acceptable by other languages,
> and everything I am reading is about importing from .json or writing to
> .json.
>
> How could I just write to a .txt file in the format I have described?
>

Solutions without generating the Array:

File.open(city_name, 'w') |w|
  for i in start_number...end_number
    w.puts i
  end
end

File.open(city_name, 'w') |w|
  (start_number...end_number).each do |i|
    w.puts i
  end
end

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/