Separate new lines from an output

L

Leo M.

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
 
R

Robert Klemme

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
 
H

Haruka YAGNI

Hi Leo.

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

botp

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
 
L

Leo M.

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 :-\
 
P

Peter Hickman

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")}

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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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")}
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.
 
P

Phillip Gawlowski

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 Gawlowski

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

Robert Klemme

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")}

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
You have pushed an array (e) to be the first element of the array (array).

He has pushed a Range into the Array.
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
 
P

Peter Hickman

Ah bugger, got my cut and paste all wrong.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top