Separate new lines from an output

Hello!

I’ve got this problem:

I’ve a range from 1…87.

I’ve got how to print 87 lines.

I’m trying to make 87 different objects, since if i try to push this
result into an array, the array.size is always 1. How could I have 87
different objects to push in an array?

Thanks a lot for your help!

L

On Tue, Apr 5, 2011 at 11:30 AM, Leo M. [email protected] wrote:

I’ve got this problem:

I’ve a range from 1…87.

I’ve got how to print 87 lines.

I’m trying to make 87 different objects, since if i try to push this
result into an array, the array.size is always 1. How could I have 87
different objects to push in an array?

It’s not clear what you want. Care to show some code?

Cheers

robert

Hi Leo.

On Tue, Apr 5, 2011 at 6:30 PM, Leo M. [email protected] wrote:

I’m trying to make 87 different objects, since if i try to push this
result into an array, the array.size is always 1. How could I have 87
different objects to push in an array?

I might misunderstand what you want to do, but how about this?

Array.new(87) { “” } # => ["", “”, …(87 times)]

If you want to set length of the array according to some variable.

Array.new(some_variable) { “” }

Each element of array is different.

Sorry for being imprecise.

The matter is this : I have 87 files named black1, black2 … black
87.

I want ruby to print each file with the entire path, so, this is what
I’ve written :

i = 1…87

array = []

e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}

array.push e

puts array.size # => 1

ruby actually prints correctly each line, but as a single array object.

I need that each line, each path printed is a single array object, so
that in the end I’ll have an array of 87 as size with :

“/R/blackout/black1”
“/R/blackout/black2”

“/R/blackout/black87”

I also tried the .chomp method but is pretty much the same :-\

On 5 April 2011 11:38, Leo M. [email protected] wrote:

array = []

e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}

e at this point is an array

array.push e

You have pushed an array (e) to be the first element of the array
(array).

puts array.size # => 1

So this is correct. You might want to try

i = 1…87

array = i.map {|i| “/R/blackout/black”+i.to_s}

puts array.size # => 87

On Tue, Apr 5, 2011 at 5:38 AM, Leo M. [email protected] wrote:

array = []

e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}

Here, you are sending the path to standard output (that’s what puts
does,
that’s what it means to print data). But you are more interested in
using
the result that you are calculating rather than printing it. So you want
something that will collect those values into an array:

e = i.collect { |i| “/R/blackout/black”+i.to_s+"\n" }

(collect is another name for map)

alternatively, you could iterate over each element and add it to the
array

i.each { |i| array << “/R/blackout/black”+i.to_s+"\n" }

Some quick advice, you probably don’t want a newline in your filename,
so
“/R/blackout/black”+i.to_s+"\n"
should probably be
“/R/blackout/black”+i.to_s

And it is also more common to use interpolation, because it looks nicer
(IMO), is more efficient, and requires fewer characters. It’s also
easier
for my brain to comprehend at a glance:
“/R/blackout/black#{i}”

array.push e

puts array.size # => 1

Something that might be helpful is the p method.

p prints out an inspected version of your object

i = 1…87

I’ll omit the output of the puts statements

e = i.each { |i| puts “/R/blackout/black#{i}” }

p e

>> 1…87

Here, we can see that #each returns the object it was iterating over,
the
range from 1 to 87, not the new array, and certainly not what we sent to
stdout.

On Tue, Apr 5, 2011 at 5:30 PM, Leo M. [email protected] wrote:

I’m trying to make 87 different objects, since if i try to push this
result into an array, the array.size is always 1.

check the doc.
eg, try

$ ri Array#"<<"

How could I have 87
different objects to push in an array?

many ways.
eg,

a=[]
#=> []
87.times{a<<Object.new}
#=> 87
a.size
#=> 87
a.first(5)
#=> [#Object:0x937c16c, #Object:0x937c158, #Object:0x937c144,
#Object:0x937c130, #Object:0x937c11c]

best regards -botp

It works! Thanks everybody :slight_smile:

On Tue, Apr 5, 2011 at 12:38 PM, Leo M. [email protected] wrote:

Sorry for being imprecise.

The matter is this : I have 87 files named black1, black2 … black
87.

I want ruby to print each file with the entire path, so, this is what
I’ve written :

Why not use Dir#glob([pattern])?

That automatically creates an array with the files and their paths
that match [pattern].


Phillip G.

Though the folk I have met,
(Ah, how soon!) they forget
When I’ve moved on to some other place,
There may be one or two,
When I’ve played and passed through,
Who’ll remember my song or my face.

On Tue, Apr 5, 2011 at 12:59 PM, Peter H.
[email protected] wrote:

array = []

e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}

e at this point is an array

No, it’s a Range.

irb(main):001:0> (1…87).each {}
=> 1…87
irb(main):002:0> (1…87).each {}.class
=> Range

array.push e

You have pushed an array (e) to be the first element of the array (array).

He has pushed a Range into the Array.

puts array.size # => 1

So this is correct. You might want to try

i = 1…87

array = i.map {|i| “/R/blackout/black”+i.to_s}

puts array.size # => 87

If it is just for printing I’d rather

(1…87).each {|i| puts “/R/blackout/black#{i}”}
1.upto(87) {|i| puts “/R/blackout/black#{i}”}

Cheers

robert

On 5 April 2011 13:54, Robert K. [email protected] wrote:

On Tue, Apr 5, 2011 at 12:59 PM, Peter H.

e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}

e at this point is an array

Thank you for your correction.

Ah bugger, got my cut and paste all wrong.

Thank you, Robert K., for pointing out that it was a range that
was being pushed an not an array as I stated.

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